final Timer updater = new Timer(3000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// update JTextField
}
});
JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updater.start();
}
});
Timer 不会阻止事件调度线程(如 Thread.sleep ),因此不会导致程序无响应 .
2
你不能在美国东部时间睡觉 . 你可以使用swingworker(更好的解决方案)或做这样的事情:
//sleep in new thread
new Thread (new Runnable() {
public void run() {
Thread.sleep(3000);
//update UI in EDT
SwingUtilities.invokelater(new Runnable() {
public void run() {updateYourTextHere();}
});
}
}).start();
3 回答
你应该使用javax.swing.Timer .
Timer
不会阻止事件调度线程(如Thread.sleep
),因此不会导致程序无响应 .你不能在美国东部时间睡觉 . 你可以使用swingworker(更好的解决方案)或做这样的事情:
你需要在一个单独的线程上完成'the work' . 在这里看到一些答案:Java GUI Not changing