Java 使用ActionListener重新启动方法

Java 使用ActionListener重新启动方法,java,time,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; privat

我正在为我创建的游戏制作计时器,但是我很难重新启动我的计时器方法。它暂停计时器约一秒钟,然后继续计数,例如:如果计时器为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();
        }
    }
}

这在很大程度上起了作用,但当我在重置后单击“开始”按钮时,什么也没有发生,它保持为0。好的,让我检查一下,同时你也可以修复它。它对我来说工作正常。请在我的帖子中再次检查代码。它不能显示0,因为在显示之前调用了
计数器+