简体   繁体   English

在swing中使用布尔变量而不是ActionListner接口

[英]Using boolean variable instead of ActionListner interface in swing

I have a small query please let me explain the scenario. 我有一个小问题,请让我解释一下这个场景。 I have a swing jframe in which i have a button named "start" which starts the timer in seconds so whenever i click on start it converts the the button itself to "reset" which should make the seconds to zero and should again convert itself to "start". 我有一个摆动jframe,其中我有一个名为“start”的按钮,它在几秒钟内启动计时器,所以每当我点击开始它就会将按钮本身转换为“重置”,这应该使秒为零,并且应该再次将自身转换为“开始”。 My concern is for these both scenarios i have to run two set of codes for which i used two classes which implements ActionListener interface is there any way to include these two set of codes in the same class which implements ActionListener, and switch the block of code depending on a boolean variable which changes its value as the button changes. 我关心的是这两个场景我必须运行两组代码,我使用了两个实现ActionListener接口的类,有没有办法在实现ActionListener的同一个类中包含这两组代码,并切换代码块取决于一个布尔变量,它随着按钮的变化而改变其值。

I tried it but i am facing performance issues like freezing the application and not even working exactly as expected. 我尝试了但是我遇到了性能问题,如冻结应用程序,甚至没有完全按预期工作。

please review my code below. 请查看下面的代码。

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

Use just one ActionListener or perhaps better, AbstractAction, give it a boolean variable, and then have it change its state based on the boolean variable. 只使用一个ActionListener或者更好的AbstractAction,给它一个布尔变量,然后让它根据布尔变量改变它的状态。

eg, 例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM