我想在每個 if 之間設置一個延遲,因為我已經使用 Thread.sleep() 進行了嘗試,但這會凍結 gui,我不知道在循環中使用多個搖擺計時器是否可行。、在這里,我正在嘗試使用搖擺計時器并一直凍結 gui,我做錯了什么?int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { int i=0; public void actionPerformed(ActionEvent evt) { try { System.out.print(solucion.get(i)+" "+solucion.get(i+1)+" "+solucion.get(i+2)+" \n"+solucion.get(i+3)+" "+solucion.get(i+4)+" "+solucion.get(i+5)+" \n"+solucion.get(i+6)+" "+solucion.get(i+7)+" "+solucion.get(i+8)); System.out.println("\n"); Btn1.setText(solucion.get(i)); Btn2.setText(solucion.get(i+1)); Btn3.setText(solucion.get(i+2)); Btn4.setText(solucion.get(i+3)); Btn5.setText(solucion.get(i+4)); Btn6.setText(solucion.get(i+5)); Btn7.setText(solucion.get(i+6)); Btn8.setText(solucion.get(i+7)); Btn9.setText(solucion.get(i+8)); i++; } catch(IndexOutOfBoundsException e){((Timer)evt.getSource()).stop();} //if it gets a error we are at the end of the list and stop the timer } }; new Timer(delay, taskPerformer).start();
2 回答

慕尼黑5688855
TA貢獻1848條經驗 獲得超2個贊
使用Swing Timer
. 計時器替換循環。
每次 Timer 觸發時,您都會設置文本,然后增加“i”的值。當“i”達到特定值時,您將停止計時器。
請參閱:Jlabel 顯示新舊數字的簡單示例,以幫助您入門。
閱讀 Swing 教程中有關如何使用 Swing 計時器的部分以獲取更多信息。

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
如果您希望 guid 不凍結,則需要在不同的線程中執行它。在主線程中運行它會導致 guid 凍結。您正在使用搖擺,所以要走的路是:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// put your statements and delay here
}
});
添加回答
舉報
0/150
提交
取消