发生的事情是3秒代码在GUI线程中执行,因此按钮在完成之前没有机会更新.
要解决这个问题,请启动SwingWorker进行长时间运行;那么你在等待它时仍然可以自由地在GUI中做事.
以下是关于该主题的0700的couple,上面引用的SwingWorker Javadoc也有一些代码.
示例代码
public void actionPerformed(ActionEvent e) {
SwingWorker worker = new SwingWorker() {
@Override
public Void doInBackground() {
// Call complicated code here
return null;
// If you want to return something other than null, change
// the generic type to something other than Void.
// This method's return value will be available via get() once the
// operation has completed.
}
@Override
protected void done() {
// get() would be available here if you want to use it
myButton.setText("Done working");
}
};
myButton.setText("Working...");
worker.execute();
}