我做了一个计时器,当它为 0 时,我想改变框架。它可以工作,但相同的框架会不断弹出并且不会停止。
查看 if 和 else 部分。
class SetTimer {
private static final int TIMER_PERIOD = 1000;
protected static final int MAX_COUNT = 5;
private GameLuncher info;
private int count;
public SetTimer(GameLuncher gameLuncher) {
this.info = gameLuncher;
String text = " " + (MAX_COUNT - count) + " ";
gameLuncher.setCountDownLabelText(text);
}
public void start() {
new Timer(TIMER_PERIOD, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (count < MAX_COUNT) {
count++;
String text = " " + (MAX_COUNT - count) + " ";
info.setCountDownLabelText(text);
} else {
((Timer) e.getSource()).stop();
new GameLuncher().setVisible(false);
new MainFrame().setVisible(true);
}
}
}).start();
}
}