1

我有一个小问题,请让我解释一下这个场景。我有一个swing jframe,其中我有一个名为“start”的按钮,它在几秒钟内启动计时器,所以每当我点击开始时,它会将按钮本身转换为“reset”,这应该使秒数为零,并且应该再次将自身转换为“开始”。我担心这两种情况我必须运行两组代码,我使用了两个实现 ActionListener 接口的类有没有办法将这两组代码包含在实现 ActionListener 的同一个类中,并切换代码块取决于一个布尔变量,该变量会随着按钮的变化而改变其值。

我试过了,但我遇到了性能问题,比如冻结应用程序,甚至不能完全按预期工作。

请在下面查看我的代码。

 public class SuperTimer extends JFrame
{
    JButton start;
    private final StartCountDown countdown;
    public SuperTimer()
    {
        countdown= new StartCountDown();
        start.addActionListener(countdown);
    }
    public class StartCountDown implements ActionListener
    {
        public void actionPerformed(ActionEvent l)
        {
            if(l.getSource()==start)
            {
                count = Long.parseLong(input.getText());
                start.setText("Reset");
                reset = new Reset();
                start.removeActionListener(countdown);
                start.addActionListener(reset);
                invalid.setVisible(false);
            }
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000,tc);
            timer.start();
        }
    }
    public class Reset implements ActionListener
    {
        public void actionPerformed(ActionEvent j)
        {
            start.setText("Start");
            time.setText("00:00:00");
            input.setText("");
            timer.stop();
            timeup.setVisible(false);
            start.removeActionListener(reset);
            start.addActionListener(countdown);
        }
    }
}**
4

1 回答 1

3

只使用一个 ActionListener 或者更好的 AbstractAction,给它一个布尔变量,然后让它根据布尔变量改变它的状态。

例如,

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

public class ActionWithMultipleBehaviors extends JPanel {
   private TimerButtonAction timerBtnAction = new TimerButtonAction("Start", "Reset");
   private JButton timerButton = new JButton(timerBtnAction);

   public ActionWithMultipleBehaviors() {
      add(timerButton);
   }

   class TimerButtonAction extends AbstractAction {
      private boolean stateStart = true;
      private String name;
      private String secondName;

      public TimerButtonAction(String name, String secondName) {
         super(name);
         this.name = name;
         this.secondName = secondName;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         String newName;
         if (stateStart) {
            newName = secondName;

            // TODO: add start timer code

         } else {
            newName = name;

            // TODO: add reset timer code

         }
         putValue(NAME, newName);
         stateStart = !stateStart;
      }
   }

   private static void createAndShowGui() {
      ActionWithMultipleBehaviors mainPanel = new ActionWithMultipleBehaviors();

      JFrame frame = new JFrame("ActionWithMultipleBehaviors");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2014-10-02T23:11:28.507 回答