如何在Timer程序中实现actionlistener?

时间:2011-08-13 13:04:20

标签: java swing timer actionlistener

我需要这个计时器程序无限运行,直到有人按下重置按钮。只需单击一下按钮,该计时器程序就会递增或递减。例如,单击一次,它将增加UNTIL某人说要停止或递减。问题是,我认为我已经为此制作了正确的代码,但它根本不会运行。但是必须存在逻辑错误,但似乎无法准确指出确切位置。你能说出我的代码有什么问题吗?

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

1 个答案:

答案 0 :(得分:2)

这个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。从传递到其actionPerformed方法的ActionEvent对象返回的getSource将是其事件触发事件的对象,此处计时器不是按钮。因此,您无法从此ActionEvent对象中检索按下了哪个按钮。
  • 相反,您需要从传递给“event”类的actionPerformed方法的ActionEvent对象返回的getSource()中检索该信息。
相关问题