Use javax.swing.Timer to make countdown timer in Java [duplicate]

雨燕双飞 提交于 2019-12-20 03:21:48

问题


Possible Duplicate:
Stop timer with conditional only works first time?

I'm very confused as to how to make a timer using the swing and not util timer.

I am making a game where users have to answer questions in a 30 second time limit. I have a PlayFrame, where the time is shown, and a method inside PlayFrame called startTimer which contains all the timer stuff.

public static void startTimer() {

    int elapsedSeconds = 0;
    javax.swing.Timer myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
    elapsedSeconds++;

    if (elapsedSeconds == 30) {
        myTimer.stop();
        timerLabel.setText("0");
        wrong();
    } else {
        String text = String.format("f", 30 - elapsedSeconds);
        timerLabel.setText(text);
    }

    if (myTimer != null && myTimer.isRunning()) {
        myTimer.stop();
        myTimer = null;
        timerLabel.setText("0");
    } else {
        elapsedSeconds = 0;
        myTimer = new javax.swing.Timer(1000, new MyTimerActionListener());
        myTimer.start();
        String text = String.format("t", 30);
        timerLabel.setText(text);
    }
}

What I want this method to do is have a timer that counts down from 30 until the question is answered correctly. If the answer is answered incorrectly I want the timer to stop.

For an answer perhaps some psuedocode (or real code) to move me in the right direction. And this is for personal use, not homework or anything.

Please remember that this is a part of my whole code and it needs to work with other parts of it. I'll give more information upon request, thanks!

EDIT: New startTimer() method NOTE all System.out.print is for testing only:

  public static void startTimer() {
    class TimerListener implements ActionListener {
        Timer timer = new Timer(1000, new TimerListener());
        int elapsedSeconds = 30;
        String seconds = Integer.toString(elapsedSeconds);

        @Override
        public void actionPerformed(ActionEvent evt) {
            timer.start();
            if (elapsedSeconds == 0) {
                //System.out.print("here");
                timer.stop();
                PlayFrame.wrong();
            }
            else{
                //System.out.print("hersfde");
                elapsedSeconds--;
            PlayFrame.timerLabel.setText(seconds);
            }
            //System.out.println(elapsedSeconds);
        }
    }
    //System.out.print("l");
}

Doesn't do anything and not sure why.


回答1:


This is how I would make a countdown timer:

Timer timer = new Timer(1000, new TimerListener());

class TimerListener implements ActionListener{
    int elapsedSeconds = 30;

    public void actionPerformed(ActionEvent evt){
        elapsedSeconds--;
        timerLabel.setText(elapsedSeconds)
        if(elapsedSeconds <= 0){
            timer.stop();
            wrong()
            // fill'er up here...
        }
    }

}


来源:https://stackoverflow.com/questions/14405773/use-javax-swing-timer-to-make-countdown-timer-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!