我想知道如何在Java中的Swing應用程序中添加時間延遲,我使用了Thread.sleep(time)
,並且我也使用了SwingWorker,但它不起作用。這裏是我的代碼部分:如何在Java Swing應用程序中添加簡單的延遲?
switch (state) {
case 'A':
if (charAux == 'A') {
state = 'B';
//Here's where I'd like to add a time delay
jLabel13.setForeground(Color.red);
break;
} else {
//Here's where I'd like to add a time delay
jLabel12.setForeground(Color.red);
break;
}
}
我希望你能幫助我或者我用的SwingWorker解決我的疑惑。
首先,永遠不要把延遲到事件調度線程,並始終只更新從與UI的例子EDT。其次,我會看看[javax.swing.Timer](http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html)和[示例](http ://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – MadProgrammer