JAVA扫雷的计时问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 07:19:26
我在扫雷里面添加了一个计时器,当我按一下button时,计时器能按每秒加1计时,但我按了多次botton事件时,计时器就乱了,计时速度就加快了很多,不知道什么原因,请路过的高手帮帮忙。计时器代码如下:
public void startTimer(){
new java.lang.Thread(new Runnable()
{
public void run()
{
while(running){
try{
Thread.sleep(1000);
}
catch(Exception e){}
time+=1;
//DecimalFormat three=new DecimalFormat("000");
field2.setText(String.valueOf(time));

if(time>1000*1000){
running=false;
}
}
}
}).start();
}

我想说 你点击了一下就启动一个线程?
-------------------------------------
这样的话 你就给这个在

synchronized(this){
time+=1;
field2.setText(String.valueOf(time));

if(time>1000*1000){
running=false;
}
}