【问题标题】:How can I create a single ActionListener for multiple JButtons如何为多个 JButton 创建单个 ActionListener
【发布时间】:2013-06-05 03:50:24
【问题描述】:

我正在使用 MVC 创建一个基本计算器。到目前为止,我正在改编一个仅将两个用户输入的值相加的教程。

目前,我添加到视图中的每个按钮都有自己的侦听器,这没关系。但是,根据教程,控制器每个按钮都有一个 ActionListener 内部类。这重复了大量的代码。

如何为所有按下的按钮创建一个 ActionListener 类,并在按下的按钮的 id 上使用 case 语句?

在视图中注册 oneButton

void oneListener(ActionListener listenForOneButton){    
    oneButton.addActionListener(listenForOneButton);
}    

为Controller内部类中的oneButton实现ActionListener

class oneListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        int previousNumber, displayNumber = 0;          
        try{
            previousNumber = theView.getPreviousDisplayNumber();
            displayNumber = previousNumber+1;               
            theView.setDisplayNumber(displayNumber);
        }           
        catch(NumberFormatException ex){                
            System.out.println(ex);             
            theView.displayErrorMessage("You Need to Enter Integers");              
        }           
    }
}

【问题讨论】:

    标签: java swing jbutton actionlistener


    【解决方案1】:
    public class SingleActionListener implements ActionListener {
        public void initializeButtons() {
            JButton[] buttons = new JButton[4];
            String[] buttonNames = new String[] {"button1", "button2", "button3", "button4"};
    
            for (int i = 0; i < 4; i++) {
                buttons[i] = new JButton(buttonNames[i]);
            }
        }
    
        public void addActionListenersToButtons() {
            for (int i = 0; i < 4; i++) {
                buttons[i].addActionListener(this);
            }
        }
    
        public void actionPerformedd(ActionEvent actionEvent) {
            if (actionEvent.getSource() == buttons[0]) {
                //Do required tasks.
            }
    
            if (actionEvent.getSource() == buttons[1]) {
                //Do required tasks.
            }
    
            if (actionEvent.getSource() == buttons[2]) {
                //Do required tasks.
            }
    
            if (actionEvent.getSource() == buttons[3]) {
                //Do required tasks.
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      从实现ActionListener的类开始...

      public class CalculatorHandler implements ActionListener{
      
          public static final String ADD_ACTION_COMMAND = "Action.add";
      
          public void actionPerformed(ActionEvent e){
      
              if (ADD_ACTION_COMMAND.equals(e.getActionCommand()) {
                  // Do your addition...
              } else if ...
      
          }
      }
      

      最好将此类可以处理的动作命令定义为常量,从而消除歧义...

      接下来,在保存按钮的类中,创建ActionListener...的实例...

      CalculatorHandler handler = new CalculatorHandler();
      

      然后像往常一样创建按钮并注册handler...

      JButton plus = new JButton("+");
      plus.setActionCommand(CalculatorHandler.ADD_ACTION_COMMAND);
      plus.addActionListener(handler);
      

      恕我直言,这种方法的唯一问题是它可以创建一个怪物if-else 语句,这可能会变得难以维护。

      在我看来,我会创建某种模型/构建器,其中包含一系列辅助方法(如 add(Number)subtract(Number) 等),并为每个按钮的单独操作使用 Actions API...但这只是我……

      【讨论】:

      • +1 表示Action;以DefaultEditorKitStyledEditorKit 为例。
      猜你喜欢
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2021-02-08
      • 2015-07-18
      相关资源
      最近更新 更多