我做了一个倒计时计时器,并且假设“停止”按钮可以停止倒计时并重置文本字段。
class Count implements Runnable {
private Boolean timeToQuit=false;
public void run() {
while(!timeToQuit) {
int h = Integer.parseInt(tHrs.getText());
int m = Integer.parseInt(tMins.getText());
int s = Integer.parseInt(tSec.getText());
while( s>=0 ) {
try {
Thread.sleep(1000);
}
catch(InterruptedException ie){}
if(s == 0) {
m--;
s=60;
if(m == -1) {
h--;
m=59;
tHrs.setText(Integer.toString(h));
}
tMins.setText(Integer.toString(m));
}
s--;
tSec.setText(Integer.toString(s));
}
}
tHrs.setText("0");
tMins.setText("0");
tSec.setText("0");
}
public void stopRunning() {
timeToQuit = true;
}
}
stopRunning()
当按下“停止”按钮时我会打电话。它行不通。
还有,我说的stopRunning()
对吗??
public void actionPerformed(ActionEvent ae)
{
Count cnt = new Count();
Thread t1 = new Thread(cnt);
Object source = ae.getSource();
if (source == bStart)
{
t1.start();
}
else if (source == bStop)
{
cnt.stopRunning();
}
}