【问题标题】:Use one action listener for all buttons [duplicate]为所有按钮使用一个动作监听器[重复]
【发布时间】:2013-08-08 01:17:48
【问题描述】:

例如,我的框架中有 10 个按钮。 我可以为所有这些都使用一个独特的动作监听器吗? 我的意思不是为每个人定义听众,而是为所有人定义一个听众。 请举个例子。 谢谢

【问题讨论】:

  • 有可能...但是您需要使用switch 块或一堆if-then-else 块才能执行适合按钮的代码。该解决方案脆弱、难以维护且难以阅读。此外,它没有提供任何性能优势。没有充分的理由使用这种模式。每个按钮的单个方法或单个函数对象是一个更好的主意。一个可能的例外是所有按钮都执行相同的代码,但即便如此,最好在私有辅助方法中实现共享代码。
  • 对第一个问题“是”,第二个问题是“去试试看有什么问题再回来”。

标签: java


【解决方案1】:

您可以这样做,但是您必须将所有这些呼叫解复用到控制所有呼叫的“一个控制器”,除非您有所有按钮都应该执行相同操作的独特情况。

这种多路分解可能会涉及一个开关(或更糟糕的是,一堆 if / then 语句),并且该开关将成为维护方面的难题。见examples of how this could be bad here

【讨论】:

    【解决方案2】:

    是的,当然可以

    class myActionListener implements ActionListener {
        ...
        public void actionPerformed(ActionEvent e) {
            // your handle button click code
        }
    }
    

    在 JFrame 中

    myActionListener listener = new myActionListener ();
    button1.addActionListener(listener );
    button2.addActionListener(listener );
    button3.addActionListener(listener );
    

    等等

    【讨论】:

    • 注意:您可以使用if(e.getSource() == button1)if(e.getActionCommand().equals("Button 1"))(如果为按钮提供任何文本,例如new JButton("Button 1");)来确定按下了哪个按钮。
    【解决方案3】:
    // Create your buttons.
    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");
    // ...
    
    // Create the action listener to add to your buttons.
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // Execute whatever should happen on button click.
      }
    }
    
    // Add the action listener to your buttons.
    button1.addActionListener(actionListener);      
    button2.addActionListener(actionListener);
    button3.addActionListener(actionListener);
    // ...
    

    【讨论】:

      【解决方案4】:

      检查ActionEvent 上的source,并对每个按钮进行相等性检查,以查看导致ActionEvent 的原因。然后,您可以对所有按钮使用单个侦听器。

          final JButton button1 = new JButton();
          final JButton button2 = new JButton();
          ActionListener l = new ActionListener() {
              public void actionPerformed(final ActionEvent e) {
                  if (e.getSource() == button1) {
                      // do button1 action
                  } else if (e.getSource() == button2) {
                      // do button2 action
                  }
              }
          };
          button1.addActionListener(l);
          button2.addActionListener(l);
      

      【讨论】:

        【解决方案5】:

        您可以这样做,在actionPerformed 方法中获取操作命令并为每个按钮设置操作命令

        ActionListener al_button1 = new ActionListner(){
                @Override
                public void actionPerformed(ActionEvent e){
                    String action = e.getActionCommand();
                    if (action.equals("button1Command")){
                            //do button 1 command
                    } else if (action.equals("button2Command")){
                            //do button 2 command
                    }
                    //...
        
                }
         }
         //add listener to the buttons
         button1.addActionListener(al_buttons)
         button2.addActionListener(al_buttons)
         // set actioncommand for buttons
         button1.setActionCommand("button1Command");
         button2.setActionCommand("button2Command");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-24
          • 1970-01-01
          • 2013-03-01
          • 1970-01-01
          • 2014-04-25
          • 2023-04-06
          • 2017-03-08
          相关资源
          最近更新 更多