试图写一个计时器进行倒计时(如火箭发射:3-2-1-Go).我所拥有的似乎只执行一次.我需要它重复(几乎递归地)执行,直到值达到0.
正如您将看到的,我有各种println语句来跟踪这一进展.这是我的输出:
in Coundown constructor
in ActionListener
counting down
3
错在于我错过了以下输出:
2
1
Go
这告诉我这个计时器实际上并没有倒计时.它似乎在等待一秒然后终止.
如何在定时器达到零之前调用它?谢谢!
public class StopWatch {
JFrameMath myTest;
int seconds;
public StopWatch(JFrameMath thisTest, int sec) {
myTest = thisTest;
seconds = sec;
myTest.hideTestButtons(true);
Countdown display = new Countdown(myTest);
}
}
class Countdown extends JFrame implements ActionListener {
private Timer myTimer = new Timer(250, this);
JFrameMath myTest;
public Countdown(JFrameMath thisTest) {
System.out.println("in Coundown constructor");
myTimer.setInitialDelay(1150);
myTest = thisTest;
myTimer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("in ActionListener");
int countSeconds = 3;
if(countSeconds == 0) {
myTest.showTimeRemaining("Go");
myTimer.stop();
System.out.println("done");
} else {
System.out.println("counting down");
myTest.showTimeRemaining(""+countSeconds);
countSeconds--;
}
myTimer.stop();
myTest.hideTestButtons(false);
}
}
public void showTimeRemaining(JFrameMath thisTest, String numSec) {
System.out.println(numSec);
lblCountdown.setText(numSec);
thisTest.pack();
}