【问题标题】:Restarting a method with ActionListener使用 ActionListener 重新启动方法
【发布时间】:2014-04-25 23:41:26
【问题描述】:

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

【问题讨论】:

    标签: java time actionlistener


    【解决方案1】:

    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,因为在显示之前调用了counter++
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多