java - 如何在2个定时器之间互换?在 Java 图形用户界面中

标签 java swing timer return

好的,基本上我正在尝试创建一个可向上和向下计数的计时器。我需要该程序在任何时间只激活一个计时器。有两个计时器,一个使变量递增,另一个使变量递减。我似乎无法正确理解,当我按下增量时,变量会增量但永远不会停止,即使我按下减量按钮也是如此。我该怎么做呢?另外,另一个简单的问题:如何返回按键方法内的值?按键默认是无效的,所以我被难住了。

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class TimerTutorial extends JFrame {

    JLabel timerLabel;
    JButton buttonAdd, buttonMin, buttonReset;
    Timer timer;
    Timer timer2;

    public TimerTutorial() {
        setLayout(new GridLayout(2, 2, 5, 5));

        buttonReset = new JButton("Press to reset");
        add(buttonReset);

        buttonAdd = new JButton("Press to Add");
        add(buttonAdd);

        buttonMin = new JButton("Press to Minus");
        add(buttonMin);

        timerLabel = new JLabel("Waiting...");
        add(timerLabel);

        event e = new event();
        buttonAdd.addActionListener(e);
        buttonMin.addActionListener(e);
    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == buttonAdd) {

                TimeClassAdd tcAdd = new TimeClassAdd();
                timer = new Timer(1000, tcAdd);
                timer.start();

            } else if (e.getSource() == buttonMin) {
                TimeClassMin tcMin = new TimeClassMin();
                timer2 = new Timer(1000, tcMin);
                timer2.start();


            } else if (e.getSource() == buttonReset) {
                timer.stop();
                timer2.stop();
                // This code does not work
                // Need to revert counter to 0.
            }
        }
    }

    public class TimeClassAdd implements ActionListener {

        int counter = 0;

        public void actionPerformed(ActionEvent f) {

            String status_symbol[] = new String[4];
            status_symbol[0] = "Unused";
            status_symbol[1] = "Green";
            status_symbol[2] = "Yellow";
            status_symbol[3] = "Red";

            if (counter < 3) {
                counter++;
                timerLabel.setText("Time left: " + status_symbol[counter]);
            } else {
                timerLabel.setText("Time left: " + status_symbol[counter]);
            }
        }
    }

    public class TimeClassMin implements ActionListener {

        int counter = 4;

        public void actionPerformed(ActionEvent d) {
            String status_symbol[] = new String[4];
            status_symbol[0] = "Unused";
            status_symbol[1] = "Green";
            status_symbol[2] = "Yellow";
            status_symbol[3] = "Red";

            if (counter >= 3) {
                counter = 3;
                timerLabel.setText("Time left: " + status_symbol[counter]);
                counter--;
            } else if (counter == 2) {
                timerLabel.setText("Time left: " + status_symbol[counter]);
                counter--;
            } else if (counter == 1) {
                timerLabel.setText("Time left: " + status_symbol[counter]);
            }
        }
    }

    public static void main(String args[]) {
        TimerTutorial gui = new TimerTutorial();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(500, 250);
        gui.setTitle("Timer Tutorial");
        gui.setVisible(true);
    }
}

最佳答案

如果您启动第二个计时器,如果第一个计时器仍在运行,您肯定必须停止它(即在 timer2.stop() 之前调用 timer.start() ,反之亦然)。

否则两者都会干扰,即它们访问相同的字段(在本例中为 timerLabel )。根据计时,如果第二个计时器连续增加该值,则这可能看起来像。如果例如增加计时器总是在减少计时器之后不久触发,输出值将始终为 3 - Red 。计数器本身没有增加,但标签一遍又一遍地填充该值,因此看起来它完全忽略了递减的计时器。

尽管如此,如果每个计时器的计数器达到最终值,您也应该停止它。没有必要让它再运行了。

关于第二个问题:您不能分配返回值,而是修改监听器的某些字段,然后您可以在操作方法之外访问该字段。

关于java - 如何在2个定时器之间互换?在 Java 图形用户界面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6971334/

相关文章:

c - 如何将指针参数传递给CMSIS-RTOS虚拟定时器中的回调函数

jsp - JSP/Servlet 中的倒计时器

java - Java.math.BigInteger.and() 是如何工作的?

Java IO、输入流和输出流测试

java - 如何修复这段代码中烦人的白色飞溅?

java - 如何从一个jpanel到另一个jpanel画一条线

java - JFrame 和图形的深度 (Java)

javascript - 网页的通用循环倒计时器

java - 如何使用外键类型从 jpa 存储库返回选择查询

java - JScrollPane - 新行上的多个图像