[問題] 從listener執行run卻沒正常執行

看板java作者 (Bacon)時間15年前 (2010/05/12 18:04), 編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
這是一個倒數計時器的程式,我用按鈕觸發事件去啟動倒數計時的 thread啟動,但按鈕按下去後,計時器的確有跑(文字介面),但卻 沒有同步更新Label的數值,且按鈕也是卡在按下去的狀態,直到計 時器結束才彈起來,且Label也才更新為什麼? 程式碼: //觸發的程式 public void actionPerformed(ActionEvent e){ //set time觸發,則新建timer及設定時間 if(timeset == (JButton)e.getSource()){ game1 = new Timer(Integer.parseInt(totalH.getText()), Integer.parseInt(totalM.getText()), time1); game2 = new Timer(Integer.parseInt(totalH.getText()), Integer.parseInt(totalM.getText()), time2); game1.run(); } else{ } } 計時器的class class Timer extends Thread{ int h=0; int m=0; JLabel player; Timer(int hours, int minutes,JLabel pp){ h = hours; m = minutes; player = pp; player.setText(h+": "+m+": 00"); } public void run(){ while(h>=0){ while(m>0){ m--; for(int i=59 ; i>=0 ; i--){ //更新Label值為目前倒數秒數 player.setText(h+": "+m+":"+i); //將目前倒數秒數印出在文字介面 System.out.printf("%2d: %2d: %2d\n",h,m,i); try{ sleep(10); } catch(InterruptedException e){} } } h--; m = 60; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.20.177

05/12 19:30, , 1F
game1.start();
05/12 19:30, 1F

05/12 19:39, , 2F
原來是這樣 Orz....看來我還是自砍下面那篇文好了
05/12 19:39, 2F

05/12 19:41, , 3F
不要阿~你提供文章 還有板上文章還很有用~我還沒記錄起
05/12 19:41, 3F

05/12 19:44, , 4F
#1AHoSjmJ & http://tinyurl.com/2szjzn 我的blog就算了 XD
05/12 19:44, 4F
文章代碼(AID): #1Bwdqoxb (java)