这段代码确实有效,但我认为它可以用不同的方式编写。我有一个自定义 JDialog 用于显示不确定的进度条。对,我在我的 while 循环中设置了标题、可见性和位置。虽然这可行,但我认为它应该显示在我创建工人的位置下方,但是当我这样做时,它会给我一个非法的类型错误开始。有任何想法吗?谢谢。
SwingWorker worker = new SwingWorker<Void, Void>(){
//I am thinking this should work, but it doesn't
// Progress.setTitle("Loading Repository");
// Progress.setLocationRelativeTo(null);
// Progress.setVisible(true);
@Override
protected Void doInBackground() throws Exception {
while (runLoad.getState() != Thread.State.TERMINATED && !isCancelled()) {
try {
Progress.setTitle("Loading Repository");
Progress.setLocationRelativeTo(null);
Progress.setVisible(true);
synchronized (this) {
Thread.sleep(2000);
}
} catch (InterruptedException e) {
JOptionPane.showMessageDialog(null,
"Process interrupted: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
return null;
}
@Override
public void done() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Progress.setVisible(false);
Progress.dispose();
}
});
}
};
worker.execute();