java - 使用 ActionListener 重新启动方法

标签 java time actionlistener

我正在为我创建的游戏制作计时器,但我很难重新启动我的计时器方法。它将计时器暂停大约一秒钟,然后继续计数,例如:如果计时器处于 4,如果按下重置按钮,计时器将在 4 处暂停一秒钟,然后恢复到 5、6 等。无论如何要解决此问题?

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();
        }
    }
}

最佳答案

MyTimer类中创建一个全局计数器

static volatile int counter;

....

class event implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // handle the condition if start button is clicked 
        // more than once continuously.
        if (timer == null) {
            TimeClass tc = new TimeClass();
            timer = new Timer(1000, tc);
        }
        timer.start();
    }
}

class TimeClass implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        counter++;
        timeDisplay.setText("Elapsed Time in Seconds: " + counter);
    }
}

class event1 implements ActionListener {
    public void actionPerformed(ActionEvent c) {
        // handle the condition if stop is clicked before starting the timer
        if (timer != null) {
           timer.stop();
        }
    }
}

class event2 implements ActionListener {
    public void actionPerformed(ActionEvent d) {
        // reset the counter
        counter = 0;
        // handle the condition if reset is clicked before starting the timer
        if (timer != null) {
            timer.restart();
        }
    }
}

关于java - 使用 ActionListener 重新启动方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23304792/

相关文章:

Java——对数组进行排序

java - 自定义组件不显示

java - 错误的 SQLite 错误?代码14

c - 在 C 编程中将当前日期和时间转换为 2 个不同的变量

python - 如何在 discord.py 中安排一个函数在每天的特定时间运行?

java - 如何实现ActionListener

java - 如何使用 Java 按字母顺序对链表进行排序?

time - 从日期 postgres 中提取周数

java - JButtons 没有正确显示 borderlayout,还有向按钮添加 actionListener 的问题

javascript - 如何从键盘输入文本并将其存储到变量中?