我以编程方式创建了一个向导。它包含 3 个面板。第二个是devicePane,第三个是detailsPane。第三个面板包含一个进度条。我希望我的程序启动一个功能process()
显示第三个面板后?可以使用线程吗?
else if(ParserMainDlg.this.POSITION==1){
if(sqlConnectionPane.executeProcess()==true){
devicePane.setDeviceList();
ParserMainDlg.this.POSITION++;
fireStateChanged(oldValue);
}
}
else if(ParserMainDlg.this.POSITION==2){
System.out.println("position:"+ParserMainDlg.this.POSITION);
if(devicePane.executeProcess()==true){
ParserMainDlg.this.POSITION++;
fireStateChanged(oldValue);
}
I want sqlConnectionPane.executeProcess()
调用在显示 devicePane 面板后开始执行的函数?
您绝对可以使用线程来执行任务,这是处理长时间运行的任务的首选方式。
您在这里有多种选择。所有选项都包括回调您的向导,以更新进度条。
您可以创建自己的任务类来完成此任务,或者您可以使用现有的摇摆工人 http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html。 “SwingWorker 本身是一个抽象类;您必须定义一个子类才能创建 SwingWorker 对象;匿名内部类通常可用于创建非常简单的 SwingWorker 对象。”
使用我们刚刚了解到的 Swing Worker,您可以使用如下内容:
SwingWorker<Integer, Integer> backgroundWork = new SwingWorker<Integer, Integer>() {
@Override
protected final Integer doInBackground() throws Exception {
for (int i = 0; i < 61; i++) {
Thread.sleep(1000);
this.publish(i);
}
return 60;
}
@Override
protected final void process(final List<Integer> chunks) {
progressBar.setValue(chunks.get(0));
}
};
backgroundWork.execute();
请注意,您必须将任务分解为更小的部分才能真正显示进度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)