我正在使用窗口生成器设计java应用程序的界面。我需要做的是.. 单击按钮会执行两件事 1. 执行一些后台任务 2. 在此过程中,在新窗口中显示不确定的进度条。 我知道我需要多线程来完成此任务。
我尝试借助一些教程,但无法实现。
有人可以帮忙吗?
代码: 从我想打开进度条窗口的地方开始的函数
public void mouseDown(MouseEvent e) {
pbar p=new pbar();
p.caller();
dowork();
p.closeprogress();
}
进度条类
import org.eclipse.swt.widgets.Display;
public class pbar {
protected Shell shell;
public void pcaller() {
try {
//System.err.println("Error: " + bod);
//System.err.println("Error: " + lines);
pbar window = new pbar();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(315, 131);
shell.setText("Updating!!! Please Wait");
ProgressBar progressBar = new ProgressBar(shell, SWT.INDETERMINATE);
progressBar.setBounds(47, 34, 195, 17);
// ProgressBar pb2 = new ProgressBar(shell, SWT.HORIZONTAL |
SWT.INDETERMINATE);
// pb2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
public void close()
{
shell.close();
}
}
我希望当我调用 p.caller() 时,会出现进度条。然后,控制权应该转移到原始程序并执行 dowork() 方法。当我完成这个方法时,它将调用 p.progress.close()。 我不知道为什么不理解这个简单的程序,忘记回答。
最佳答案
可以接受使用 JFace 吗? 如果是这样,您可以使用 ProgressMonitorDialog 类。
ProgressMonitorDialog dialog = new ProgressMonitorDialog(parent.getShell());
dialog.run(true, true, new SomeTask());
...
class SomeTask implements IRunnableWithProgress {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException,
InterruptedException {
monitor.beginTask("Doing some task", IProgressMonitor.UNKNOWN);
for(int i = 0; i < 1000; i++) {
if (!monitor.isCanceled())
Thread.sleep(10);
}
monitor.done();
}
}
您可以找到完整的example of usage在这里。
关于java - swt 中新窗口中的不确定进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309434/