Call its .restart() method to .. restart it. And .stop() to stop it.
How would i stop my timer?
-
09-07-2023 - |
Question
So i made a timer class for a game ive created and i already have the start button action set to start the timer, but i am a bit confused as to how to stop the timer and to reset it back to 0. Would be great if anyone could give any solutions to my problem.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyTimer extends Panel {
private JLabel timeDisplay;
private JButton resetButton;
private JButton startButton;
private JButton stopButton;
Timer timer;
public MyTimer(){
MyTimer timer;
startButton = new JButton("Start Timer");
stopButton = new JButton("Stop Timer");
timeDisplay = new JLabel("...Waiting...");
resetButton = new JButton("Reset Timer");
this.add(resetButton);
this.add(startButton);
this.add(stopButton);
this.add(timeDisplay);
event e = new event();
startButton.addActionListener(e);
event1 c = new event1();
stopButton.addActionListener(c);
event2 d = new event2();
resetButton.addActionListener(d);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
int count = 0;
timeDisplay.setText("Elapsed Time in Seconds: " + count);
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();
}
}
public class TimeClass implements ActionListener{
int counter;
public TimeClass(int counter){
this.counter = counter;
}
public void actionPerformed(ActionEvent e){
counter++;
timeDisplay.setText("Elapsed Time in Seconds: " + counter);
}
}
class event1 implements ActionListener{
public void actionPerformed (ActionEvent c){
timer.stop();
}
}
class event2 implements ActionListener{
public void actionPerformed (ActionEvent d){
timer.restart();
}
}
}
EDIT
Ok so i have the stop button doing what it needs to do perfectly, but the reset button has a problem. it pauses the timer for a second before resuming to count up from where it was paused, ex: it would pause at 4 seconds for a second before continuing to 5, 6, etc.
Any suggestions?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyTimer extends Panel {
private JLabel timeDisplay;
private JButton resetButton;
private JButton startButton;
private JButton stopButton;
Timer timer;
public MyTimer(){
MyTimer timer;
startButton = new JButton("Start Timer");
stopButton = new JButton("Stop Timer");
timeDisplay = new JLabel("...Waiting...");
resetButton = new JButton("Reset Timer");
this.add(resetButton);
this.add(startButton);
this.add(stopButton);
this.add(timeDisplay);
event e = new event();
startButton.addActionListener(e);
event1 c = new event1();
stopButton.addActionListener(c);
event2 d = new event2();
resetButton.addActionListener(d);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
int count = 0;
timeDisplay.setText("Elapsed Time in Seconds: " + count);
TimeClass tc = new TimeClass(count);
timer = new Timer(1000, tc);
timer.start();
}
}
public class TimeClass implements ActionListener{
int counter;
public TimeClass(int counter){
this.counter = counter;
}
public void actionPerformed(ActionEvent e){
counter++;
timeDisplay.setText("Elapsed Time in Seconds: " + counter);
}
}
class event1 implements ActionListener{
public void actionPerformed (ActionEvent c){
timer.stop();
}
}
class event2 implements ActionListener{
public void actionPerformed (ActionEvent d){
timer.restart();
}
}
}
Solution
OTHER TIPS
Please have a look at sample code on How to Use Swing Timers.
use timer.restart()
to restart it again and timer.stop()
to stop it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow