java更新进度条 [英] java update progressbar
问题描述
我有一个 JFrame 和以下组件.
I have a JFrame and following components.
JButton = jButton1Progress Bar = progressBar 及其公共静态JLabel = 状态及其公共静态
JButton = jButton1 Progress Bar = progressBar and its public static JLabel = status and its public static
当按钮被点击时,就会执行不同的语句.我想在每个语句后更新我的进度条.这是我的代码
When button clicks then different statements execute. I want to update my progressbar after each statement. Here is my code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Task pbu = new Task(25, "Step 1....");
pbu.execute();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
pbu = new Task(50, "Step 2....");
pbu.execute();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
pbu = new Task(75, "Step 3....");
pbu.execute();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
pbu = new Task(100, "Done");
pbu.execute();
}
这是我用 SwingWorker 扩展的任务类
Here is my Task Class extended with SwingWorker
public class Task extends SwingWorker{
private int progress;
private String stat;
public Task(int pr, String st) {
progress = pr;
stat = st;
}
@Override
protected Object doInBackground() throws Exception {
NewJFrame1.progressBar.setValue(progress);
NewJFrame1.status.setValue(stat);
return null;
}
}
知道如何解决这个问题吗?
Any idea how can I solve this problem?
推荐答案
@Override
protected Object doInBackground() throws Exception {
NewJFrame1.progressBar.setValue(progress);
NewJFrame1.status.setValue(stat);
return null;
}
您不想从 Swing 线程更新 GUI 组件.SwingWorker
提供钩子,用于在事件调度线程上执行操作,这就是它的设计目的.从 doInBackground
调用 publish
方法并覆盖 process
以使用增量更新进行详细说明.或者,如果您只需要在完成后更新 GUI,请覆盖 done()
方法.
You don't want to update GUI components off of the Swing thread. SwingWorker
provides hooks for doing stuff back on the Event Dispatch Thread, that's what it's designed for. Call the publish
method from doInBackground
and override process
to detail with incremental updates. Or, if you only need to update the GUI when it's finished, override the done()
method.
一个非常简单的例子:
//...in some concrete implementation of SwingWorker<Void, Integer>
protected Void doInBackground() throws Exception {
//do 10% of task
doLongOp();
publish(10);
//do another 20% of task
doAnotherLongOp();
publish(20);
return null;
}
protected void process(List<Integer> pieces) {
for (Integer percent : pieces ) {
progressBar.setValue(progressBar.getValue() + percent);
}
}
编辑
@mKorbel 提出了一个很好的观点,要使上述工作正常进行,它需要扩展 SwingWorker
...Void
作为整体返回值类型(在这种情况下表示没有返回值)和 Integer
是增量更新的类型.
Edit
@mKorbel makes a good point that for the above to work it needs to extend SwingWorker<Void, Integer>
...Void
being the overall return value type (in this case indicating there's no return value) and Integer
being the type of the incremental updates.
如果工作器产生实际的最终结果(将使用 get()
检索),则可以使用该类型代替 Void
.因为我没有包含类声明,所以我忽略了这些细节.
If the worker produces an actual final result (that will be retrieved with get()
) then that type can be used in place of Void
. I had left out these details since I didn't include the class declaration.
这篇关于java更新进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!