Stop timer with conditional only works first time?

后端 未结 3 364
抹茶落季
抹茶落季 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 14:45

    Solved, thanks for the link sage88! http://albertattard.blogspot.com/2008/09/practical-example-of-swing-timer.html

    And for more help on swing timers (for future searches of this topic) http://www.asjava.com/swing/java-timer-tutorial/

    public static void startTimer() {
        listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.out.print("action");
                timerLabel.setText("" + seconds);
                seconds--;
                System.out.println(seconds);
                if (seconds < 0){
                    System.out.print("zero");
                    wrong();
                }
            }
        };
        displayTimer = new Timer(1000, listener);
        displayTimer.setInitialDelay(1);
        displayTimer.start();
    
        if (right == true){
            System.out.print("true");
            displayTimer.stop();
            right = false;
            seconds = 30;
            displayTimer = new Timer(10000, listener);
            displayTimer.setDelay(10000);
            displayTimer.setInitialDelay(1);
            displayTimer.start();
        }
        else if (right == null){
            System.out.print("null");
            displayTimer.stop();
            seconds = 30;
            displayTimer = new Timer(10000, listener);
            displayTimer.setInitialDelay(1);
            displayTimer.setDelay(10000);
            displayTimer.start();
        }
    }
    

提交回复
热议问题