摇摆计时器不停止

时间:2013-01-19 00:36:11

标签: java swing timer

我正在使用swing计时器在Netbeans中制作倒计时时钟:

public void startTimer() {

    System.out.println(right + "value");
    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            System.out.println("action");
            timerLabel.setText("" + seconds);
            --seconds;
            System.out.println(seconds);
            if (seconds == -1 && seconds < 0) {
                System.out.print("zero");
                //displayTimer.stop();
                wrong();
                dispose();
            }
        }
    };
    displayTimer = new Timer(1000, listener);
    displayTimer.setInitialDelay(100);
    displayTimer.start();

    if (right == null) {
        System.out.println("null");
    } else if (right == true) {
        System.out.println("truehere");
        displayTimer.stop();
        right = null;
        seconds = 20;
        displayTimer.setDelay(10000);
        displayTimer.setInitialDelay(100);
        displayTimer.start();
    } else if (right == false) {
        System.out.print("wrong");
        //displayTimer.stop();
        seconds = 20;
    }


}

我只是使用System.out.print来测试程序,它不是真正程序的一部分。 我调用stop()方法,但计时器继续计数。此外,我按displayTimer = new javax.swing.Timer(10000, listener);创建了一个新的计时器,但它的计数速度是原来的两倍。有人可以帮忙吗?

编辑:

这是我的计时器(一种SSCCE):

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

import javax.swing.*;

public class JavaApplication8 {
 public static void startTimer() {
    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            int seconds = 20;
            seconds--;
            System.out.println(seconds);

            if (seconds == -1 && seconds < 0) {
                System.out.print("zero");
            }
        }
    };
    Timer displayTimer = new Timer(1000, listener);
    displayTimer.setInitialDelay(100);
    displayTimer.start();
 }

public static void main(String[] args) {
    System.out.println("Type win to win!");

    startTimer();

    String read;
    Boolean right;
    int seconds;

    Scanner scanIn = new Scanner(System.in);
    read = scanIn.nextLine();

    if (read.equals("win")){
        right = true;
    }
    else{
        right = false;
    }

    if (right == true) {
        System.out.println("correct");
        //displayTimer.stop();
        right = null;
        seconds = 20;
        //displayTimer.setDelay(10000);
        //displayTimer.setInitialDelay(100);
        //displayTimer.start();
    } else if (right == false) {
        System.out.print("incorrect");
        //displayTimer.stop();
        seconds = 20;
        right = null;
    }
}
}

它不起作用,因为秒没有显示,但它确实显示20次,这就是我想要的。这只是在它自己的应用程序中,在我的真实程序中,更容易看到问题。

我注意到游戏第一次运行时效果很好。然后我再次点击播放(重置整个游戏),它的速度提高了两倍。也许我没有正确地重置一些东西?这是我的重置代码:

// Reset Everything
    PlayFrame.seconds = 20;
    PlayFrame.winnings = 0;
    PlayFrame.right = false;
    //PlayFrame.displayTimer.stop();
    PlayFrame.questionLabel.setText(null);
    PlayFrame.count = 0;
    WelcomeFrame WFrame = new WelcomeFrame();
    WFrame.setVisible(true);
    setVisible(false);                             
    PlayFrame P = new PlayFrame();
    P.dispose();

    if (PlayFrame.seconds == -1 && PlayFrame.seconds < 0){
        PlayFrame.displayTimer.stop();
    }
}

1 个答案:

答案 0 :(得分:3)

它只是伪代码,用于查看计时器如何启动和停止。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.Timer;

public class TimerOnJLabel extends JFrame {

    private static final long serialVersionUID = 1L;        
    long start = System.currentTimeMillis();
    long elapsedTimeMillis;
    int sec = 5;
    Timer timer;

    public TimerOnJLabel() {
        super("TooltipInSwing");
        setSize(400, 300);
        getContentPane().setLayout(new FlowLayout());
        final JLabel b1;
        final JRadioButton jrb = new JRadioButton();
        b1 = new JLabel("Simple tooltip 1");

        ActionListener timerTask = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                elapsedTimeMillis = System.currentTimeMillis();
                b1.setText("Timer : " + (elapsedTimeMillis-start)/1000+" ::::: " +sec);
                System.out.println("Timer working: " + sec);
                if(--sec == 0){
                    timer.stop();
                    System.out.println("Timer Stopped");
                }
            }
        };
        timer = new Timer(1000, timerTask);
        System.out.println("Timer Started");
        timer.start();

        getContentPane().add(b1);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);          
        setVisible(true);
    }

    public static void main(String args[]){
        new TimerOnJLabel();
    }
}

我希望这有帮助。