我有一个 Java 应用程序,它使用 SwingWorker 来更新 GUI 中的标签和进度条,效果很好。但是,我想为此设置添加一个功能。
我的摇摆工人执行一项具有长度的任务n
,并重复该任务m
次数。现在,我的 GUI 只告诉我任务重复了多少次,但我希望它也告诉我我们正在进行的任务的长度。说n=300
and m=50
,我想要类似的东西:
Task 49 is at 248 ()
Task has been repeated 48 times
我应该在我的 SwingWorker 中修改什么?
/**
*
* @author digitaldust
*/
public class Model extends SwingWorker<Integer, Integer> {
private HashMap<String, Number> GUIparams;
private int session;
private int ticks;
Model(HashMap<String, Number> KSMParams) {
GUIparams = KSMParams;
session = (Integer)GUIparams.get("experimentsInSession");
ticks = (Integer)GUIparams.get("howManyTicks");
}
/**
* Actual simulation
*/
@Override
protected Integer doInBackground() throws Exception {
int i=0;
while(!isCancelled() && i<session){
i++;
int ii=0;
while(!isCancelled() && ii<ticks){
// this is n, the task length and I'd like to update the GUI with this value
ii++;
}
System.out.println(i);
// this is m, how many time the task has been repeated, and now it is updated in the GUI
publish(i);
setProgress(i);
Thread.sleep(1000);
}
return i;
}
/**
* Invoked when simulation exits
*/
@Override
protected void done() {
if (isCancelled()) {
Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session cancelled by user. Closing Session...");
} else {
// do stuff
Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session ended.");
}
}
}