我正在编写一个应用程序,它可以进行一些音频分析(用作吉他调谐器,还有和弦估计以及其他一些东西),但是我在GUI上遇到了一些麻烦。第一个问题是我有一个按钮,点击后应该改变其显示的文字。当它被点击时,文本不会改变,但是应该改变它的代码行肯定是可以达到的。重复执行SwingWorker
另一件事是我需要一个SwingWorker重复执行(一旦完成后重新启动)并每次更新GUI。由于目前我的代码,我有一个while循环反复执行我的SwingWorker,但这导致GUI由于它在EDT中运行(假定)而变得无响应。让SwingWorker重复执行的最佳方式是什么?我应该只是创建一个新的线程来运行循环,或其他?
代码为我的内心的ActionListener类如下:
private class TunerListener implements ActionListener {
private boolean firstUpdate = true;
private volatile boolean executing = false;
private final SwingWorker<Void, Void> tunerWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() {
model.update();
return null;
}
@Override
protected void done() {
if (!this.isCancelled()) {
prev.setText(model.getPrev());
currentNote.setText(model.getNote());
next.setText(model.getNext());
frequency.setText(model.getFrequency());
switch (model.getOffset()) {
case -2:
light_2.setIcon(onRed);
light_1.setIcon(off);
light0.setIcon(offBig);
light1.setIcon(off);
light2.setIcon(off);
break;
case -1:
light_2.setIcon(off);
light_1.setIcon(onRed);
light0.setIcon(offBig);
light1.setIcon(off);
light2.setIcon(off);
break;
case 0:
light_2.setIcon(off);
light_1.setIcon(off);
light0.setIcon(onGreen);
light1.setIcon(off);
light2.setIcon(off);
break;
case 1:
light_2.setIcon(off);
light_1.setIcon(off);
light0.setIcon(offBig);
light1.setIcon(onRed);
light2.setIcon(off);
break;
case 2:
light_2.setIcon(off);
light_1.setIcon(off);
light0.setIcon(offBig);
light1.setIcon(off);
light2.setIcon(onRed);
break;
}
}
}
};
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("tune")) {
if (!executing) {
tune.setText("Stop Tuning");
executing = true;
while (executing) {
tunerWorker.execute();
firstUpdate = false;
}
firstUpdate = true;
} else {
tune.setText("Start Tuning");
executing = false;
tunerWorker.cancel(true);
firstUpdate = true;
}
}
}
}
编辑:按钮上的文字问题似乎已经被固定现在,但我仍然不能得到这个SwingWorker的正常工作。我已经尝试完全删除while循环,并让它从其done()方法重新执行本身,但这似乎并不能帮助您处理...
通常最好为每个问题提出一个单独的问题 – Tharwen
但是......在这种情况下,似乎问题是相同的。 – Tharwen
您能否详细说明一下?我发现,这真的不工作,我期望它如何:( – nihilo90