作者baconcsie (Bacon)
看板java
标题[问题] 从listener执行run却没正常执行
时间Wed May 12 18:04:31 2010
这是一个倒数计时器的程式,我用按钮触发事件去启动倒数计时的
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
1F:推 snowlike:game1.start(); 05/12 19:30
2F:→ tkcn:原来是这样 Orz....看来我还是自砍下面那篇文好了 05/12 19:39
3F:→ baconcsie:不要阿~你提供文章 还有板上文章还很有用~我还没记录起 05/12 19:41