Java – I can’t start the timer twice
                                        
                    •
                    Java                                    
                I'm making a timer in Java. I need help The main class adds a swing timer I have a JFrame with two panels, one has jlabel, and the other has three buttons, "start", "stop" and "reset" When I click start, then everything is normal, when I stop and then reset But when I click start again, it will throw this exception: @ h_ 403_ 7@
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)
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);
}
}
Solution
The answer is in the error message It means that you cannot start a timer that has been cancelled You must use the new operator to create a new timer@ H_ 403_ 7@
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        