java - 如何在定时器程序中实现 Action 监听器?

标签 java swing timer actionlistener

我需要这个计时器程序无限期地运行,直到有人按下重置按钮。只需单击一个按钮,该计时器程序就会递增或递减。例如,单击一次,它将递增,直到有人说停止或递减。问题是,我认为我已经为此编写了正确的代码,但它根本无法运行。肯定是有逻辑错误,但具体错误在哪里,我也说不出来。你能告诉我我的代码有什么问题吗?

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);
      buttonReset.addActionListener(e);
   }

   public class event implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         while (true) {
            TimeClassAdd tcAdd = new TimeClassAdd();
            timer = new Timer(1000, tcAdd);
            timer.start();
            timerLabel.setText("IT HAS BEGUN");
         }
      }

      public class TimeClassAdd implements ActionListener {
         int counter = 0;

         public void actionPerformed(ActionEvent e) {
            String status_symbol[] = new String[4];
            status_symbol[0] = "Unused";
            status_symbol[1] = "Green";
            status_symbol[2] = "Yellow";
            status_symbol[3] = "Red";
            if (e.getSource() == buttonAdd) {
               if (counter < 3) {
                  counter++;
                  timerLabel.setText("Time left: " + status_symbol[counter]);
               } else {
                  timerLabel.setText("Time left: " + status_symbol[counter]);
               }
            } else if (e.getSource() == buttonMin) {
               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);
   }
}

最佳答案

这个while (true)将使您的整个应用程序进入休眠状态:

     while (true) {
        TimeClassAdd tcAdd = new TimeClassAdd();
        timer = new Timer(1000, tcAdd);
        timer.start();
        timerLabel.setText("IT HAS BEGUN");
     }

根本不要在 Swing 应用程序中执行此操作,至少不要在主 Swing 事件线程上执行此操作,因为它会占用事件线程,不允许发生其他操作。此外,由于您使用的是计时器,因此根本不需要 while 循环。

编辑 1
其他问题:

  • 您的 TimeClassAdd 是计时器使用的 ActionListener。从传递到其 actionPerformed 方法的 ActionEvent 对象返回的 getSource 将是其事件触发事件的对象,这里是计时器而不是按钮。因此,您无法从此 ActionEvent 对象检索按下了哪个按钮。
  • 相反,您需要从传递到“事件”类的 actionPerformed 方法的 ActionEvent 对象返回的 getSource() 中检索该信息。

关于java - 如何在定时器程序中实现 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7050588/

相关文章:

python - 毫秒级精确 python 计时器

java - SAX 解析器问题

java - 使用 paintThumb 我已经让我的箭头在 0 或 100 时从 JSlider 上掉下来,如何解决这个问题?

java - 让jbutton打开和关闭

ios - 如果在一定时间内未交互,则隐藏按钮

java - 如何设置字体的颜色?

java - Google Guava Cache 引用值较弱,未给出驱逐通知

java - 区分 `DataIntegrityViolationException`异常

java - 无法在耳朵内访问EJB jar

java - 尝试从 Spring Boot 创建 SWING/AWT 框架时由 : java. awt.HeadlessException 引起