我不能启动计时器2次

时间:2014-02-10 16:04:13

标签: java swing timer

我正在使用Java制作计时器,我需要帮助。主类增加了一个挥杆计时器。我有一个jFrame,有2个面板,1个有jLabel,另一个有3个按钮,“开始”,“停止”和“重置”。当我点击开始然后一切正常,当我停止然后重置。但是,当我再次点击“开始”时,它会抛出这个异常:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Timer already                                       cancelled.
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.schedule(Unknown Source)
at org.stopwatch.Stopwatch.start(Stopwatch.java:71)
at org.stopwatch.Stopwatch$1.actionPerformed(Stopwatch.java:48)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是我的代码:     包org.stopwatch;

import static javax.swing.UIManager.getSystemLookAndFeelClassName;
import static javax.swing.UIManager.setLookAndFeel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stopwatch {

private static boolean running = false;
public static int time = 0;
public static Timer timer = new Timer();
public static JLabel leftLabel = new JLabel(time + "s");

public static final void main(String[] args) throws Exception {

    setLookAndFeel(getSystemLookAndFeelClassName());
    JFrame f = new JFrame();
    f.setVisible(true);
    f.setSize(1, 1);
    f.setTitle("Секундомір");
    f.setDefaultCloseOperation(3);
    f.setLocationRelativeTo(null);
    JPanel leftPanel = new JPanel();
    f.add(leftPanel, BorderLayout.NORTH);
    leftPanel.add(leftLabel);
    JPanel buttonPanel = new JPanel();
    f.add(buttonPanel, BorderLayout.SOUTH);
    JButton startBtn = new JButton("Start");
    JButton stopBtn = new JButton("Stop");
    JButton resetBtn = new JButton("Reset");
    buttonPanel.add(startBtn);
    buttonPanel.add(stopBtn);
    buttonPanel.add(resetBtn);
    startBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent a) {
            running = true;
            start();
        }
    });
    stopBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent a) {
            running = false;
            timer.cancel();
        }
    });
    resetBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent a) {
            if(!running) {
                time = 0;
                leftLabel.setText("0s");
            }
        }
    });
    f.pack();
}

public static void start() {
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            time++;
            leftLabel.setText(time + "s");
        }
    }, 1000, 1000);
}

}

4 个答案:

答案 0 :(得分:9)

答案在错误消息中。它说你不能启动已被取消的计时器。您必须使用Timer运算符创建新的new

start()功能中执行此操作。

timer = new Timer();

如果您决定采用这条路线,请不要忘记从声明中删除new操作。您将覆盖start()功能中的作业。

public static Timer timer;

如果要在运行之间存储时间,则需要在使用新计时器覆盖计时器之前执行此操作。

答案 1 :(得分:3)

是的,它的行为完全符合记录。根据{{​​3}}:

  

一旦计时器终止,它的执行线程就会正常终止,并且不再安排任何任务。

根据schedule方法的文档:

  

IllegalStateException - 如果任务已经安排或取消,计时器被取消,或计时器线程被终止。

基本上,您需要在每个Timer操作上创建新的start - 或者只是不要取消现有的操作,更改run的{​​{1}}方法当计时器被逻辑停止时忽略操作。

答案 2 :(得分:0)

你应该在start()上制作一个新的计时器,并用'旧'计时器填充它。

查看this

答案 3 :(得分:0)

当你调用timer.cancel()时,定时器线程将停止! 你可以停止一些任务,但不是计时器!当计时器取消时,你必须新建一个领带!

你可以查看this