[英]How would i stop my timer?
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. 因此,我为创建的游戏ive创建了一个计时器类,并且已经设置了开始按钮动作来启动计时器,但是我对于如何停止计时器并将其重置为0感到有些困惑。任何人都可以为我的问题提供任何解决方案。
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.
它会暂停计时器一秒,然后再从暂停的地方开始计数,例如:它将在4秒处暂停一秒,然后继续到5、6等。
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();
}
}
}
Please have a look at sample code on How to Use Swing Timers . 请查看有关如何使用Swing计时器的示例代码。
use timer.restart()
to restart it again and timer.stop()
to stop it. 使用
timer.restart()
重新启动它,并使用timer.stop()
停止它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.