亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

多個setText java之間的延遲

多個setText java之間的延遲

米脂 2022-07-06 09:46:07
我想在每個 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 計時器的部分以獲取更多信息。


查看完整回答
反對 回復 2022-07-06
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

如果您希望 guid 不凍結,則需要在不同的線程中執行它。在主線程中運行它會導致 guid 凍結。您正在使用搖擺,所以要走的路是:


SwingUtilities.invokeLater(new Runnable() {

      public void run() {

          // put your statements and delay here

      }

  });


查看完整回答
反對 回復 2022-07-06
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號