我有一个程序,它应该有一个gui,你在一个文本框中输入值,它作为计时器倒计时。线程处理倒计时,停止按钮应该停止倒计时。我有一个布尔值来设置循环是否运行。问题是,按钮更改值所做的任何努力都将被忽略。当按下按钮时,布尔值被设置为true,但是如果我随后再次检查它,它会返回false。我不明白为什么会这样。
这是倒计时法。
class countClockDown implements Runnable
{
int hour, min, sec, mili;
volatile boolean stop = false;
countClockDown()
{
}
void setCountClockDown(int a, int b, int c)
{
hour = a;
min = b;
sec = c;
}
void setCountClockDown(int a, int b, int c, int d)
{
hour = a;
min = b;
sec = c;
mili = d;
}
void stopRunning()
{
stop = true;
}
public void run()
{
while(!stop && (hour != 0 | min != 0 | sec > 0 | mili > 0))
{
Thread.yield();
if (mili < 0)
{
mili = 999;
sec--;
}
if (sec < 0)
{
sec = 59;
min--;
}
if (min < 0)
{
min = 59;
hour--;
}
mili--;
countHRIn.setText(String.valueOf(hour));
countMINIn.setText(String.valueOf(min));
countSECIn.setText(String.valueOf(sec));
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
}
}
}
}
这是按钮。
public void actionPerformed(ActionEvent ae)
{
int hour, min, sec, mil;
Object source = ae.getSource();
countClockDown cd = new countClockDown();
countClockUp cu = new countClockUp();
Thread t = new Thread(cd);
if (source == countStart)
{
hour = Integer.parseInt(countHRIn.getText());
min = Integer.parseInt(countMINIn.getText());
sec = Integer.parseInt(countSECIn.getText());
cd.setCountClockDown(hour, min, sec);
t.start();
}
if (source == countStop)
{
cd.stop = true;
t.interrupt();
System.out.print(cd.stop);
}
感谢您的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!