Stop timer with conditional only works first time?

后端 未结 3 365
抹茶落季
抹茶落季 2021-01-21 14:38

I\'m writing a \"Who Wants to Be a Millionare\" game and I have everything set up, it\'s just a problem with the timer.

The game works like this: if the user gets the qu

3条回答
  •  失恋的感觉
    2021-01-21 15:11

    You shouldn't be using java.util.Timer with a Swing application. For these you should use javax.swing.Timer since this will respect the Swing event thread. Thus your question is moot, and I suggest that you throw all of your timing code out and try the proper timer. You can find the tutorial here: Swing Timer Tutorial

    Next you'll want to get rid of all the static methods and fields that your code is using and change them over for instance fields and methods.

    An example of a Swing Timer displaying remaining time in a JLabel:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TimerExample {
    
       private static void createAndShowGui() {
          Gui mainPanel = new Gui();
    
          JFrame frame = new JFrame("TimerExample");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class Gui extends JPanel {
       private static final long serialVersionUID = 1L;
       private static final String PRESS_BUTTON = "Press Button ";
       private static final String PRESS_BUTTON_AGAIN = "Press Button Again Within %02d Seconds!";
       private static final String YOU_LOSE = "You Lose!";
       private static final String YOU_WIN = "You Win!";
       private static final int PREF_W = 250;
       private static final int PREF_H = 100;
       private static final int TOTAL_SECONDS = 20;
       private static final int ONE_SECOND = 1000;
    
       private int elapsedSeconds = 0;
       private JLabel timerLabel = new JLabel(PRESS_BUTTON);
       private JButton button = new JButton("Button");
       private Timer myTimer;
    
       public Gui() {
          JPanel btnPanel = new JPanel();
          btnPanel.add(button);
    
          setLayout(new BorderLayout());
          add(btnPanel, BorderLayout.CENTER);
          add(timerLabel, BorderLayout.SOUTH);
    
          button.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                if (myTimer != null && myTimer.isRunning()) {
                   myTimer.stop();
                   myTimer = null;
                   timerLabel.setText(YOU_WIN);               
                } else {
                   elapsedSeconds = 0;
                   myTimer = new Timer(ONE_SECOND, new TimerListener());
                   myTimer.start();
                   String text = String.format(PRESS_BUTTON_AGAIN, TOTAL_SECONDS); 
                   timerLabel.setText(text);
                }
             }
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private class TimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             elapsedSeconds++;
    
             if (elapsedSeconds == TOTAL_SECONDS) {
                myTimer.stop();
                timerLabel.setText(YOU_LOSE);
             } else {
                String text = String.format(PRESS_BUTTON_AGAIN, TOTAL_SECONDS - elapsedSeconds);
                timerLabel.setText(text );
             }
          }
       }
    }
    

提交回复
热议问题