【问题标题】:How to implement actionlistener inside a Timer program?如何在 Timer 程序中实现 actionlistener?
【发布时间】:2011-08-13 13:04:20
【问题描述】:

我需要这个计时器程序无限期地运行,直到有人按下重置按钮。只需单击一个按钮,此计时器程序就会增加或减少。例如,单击一次,它将递增直到有人说它停止或递减。问题是,我想我已经为此编写了正确的代码,但它根本无法运行。其中一定有逻辑错误,但似乎无法确定确切的位置。你能告诉我的代码有什么问题吗?

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

【问题讨论】:

标签: java swing timer actionlistener


【解决方案1】:

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

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

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

编辑 1
其他问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2011-03-04
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多