我做了一个计时器,当它为0时,我想更改帧。
它可以工作,但同一帧会不断弹出,并且不会停止。

查看if and 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();

   }

}

最佳答案

正如DavidPärsson所说,“ new GameLuncher()。setVisible(false)”不会隐藏已经创建的可见GameLuncher实例,而是创建一个新的GameLuncher并将其隐藏。

我建议 :

   ...
} else  {
   ((Timer) e.getSource()).stop();
   info.setVisible(false);
   new MainFrame().setVisible(true);
}

10-08 13:26