使用javax.swing.Timer在Java中生成倒数计时器 [英] Use javax.swing.Timer to make countdown timer in Java

查看:309
本文介绍了使用javax.swing.Timer在Java中生成倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

有条件的停止计时器仅适用于第一次?

我很惊讶如何使用swing而不是util计时器来制作计时器。

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

我正在制作一个用户必须回答的游戏在30秒的时间限制内提问。我有一个PlayFrame,其中显示时间,PlayFrame中的方法名为startTimer,其中包含所有计时器内容。

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);
    }
}

我想要这个方法做的是有一个计时器从30开始倒数​​,直到问题得到正确回答。如果答案答案不正确,我希望计时器停止。

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!

编辑:新的startTimer()方法注意所有System.out.print用于测试only:

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.

推荐答案

这就是我制作倒计时器的方法:

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...
        }
    }

}

这篇关于使用javax.swing.Timer在Java中生成倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆