I made a timer and when it is 0 I want to change frame.
It works but the same frame keeps pop up and doesn’t stops.
Check out the if and else part.
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();
}
}
As David Pärsson said, “new GameLuncher().setVisible(false)” does not hide the visible GameLuncher instance already created but creates a new GameLuncher and hides it.
I suggest :