我制作了一个计时器,当它为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();
}
}
答案 0 :(得分:3)
正如DavidPärsson所说,“新的GameLuncher()。setVisible(false)”不会隐藏已创建的可见GameLuncher实例,但会创建一个新的GameLuncher并隐藏它。
我建议:
...
} else {
((Timer) e.getSource()).stop();
info.setVisible(false);
new MainFrame().setVisible(true);
}