JPanel动作侦听器

时间:2013-11-21 18:32:47

标签: java swing actionlistener jtextfield jcheckbox

我有一个带有一堆不同复选框和文本字段的JPanel,我有一个禁用的按钮,需要在设置特定配置时启用。 我需要的是整个JPanel上的监听器,无论何时发生任何变化,都会寻找事件。 我相信我需要一个动作监听器,但我找不到任何东西来连接监听器与JPanel

JPanel Window = new JPanel();
Window.addActionListener(new ActionListener(){
//Check if configurations is good
}

我想我可以将我的代码复制并粘贴到面板中的每个监听器中,但这对我来说似乎是不好的编码习惯。

3 个答案:

答案 0 :(得分:4)

首先,在他的comment JPanel中提及@Sage是一个容器,而不是一个行动的组件。因此,您无法将ActionListener附加到JPanel

  

我想我可以将我的代码复制并粘贴到每个代码中   小组中的听众,但这对我来说似乎是不好的编码练习。

你完全正确,这根本不是一个好习惯(见DRY principle)。除此之外,您只需定义一个ActionListener并将其附加到JCheckBox es,就像这样:

final JCheckBox check1 = new JCheckBox("Check1");
final JCheckBox check2 = new JCheckBox("Check2");
final JCheckBox check3 = new JCheckBox("Check3");

final JButton buttonToBeEnabled = new JButton("Submit");
buttonToBeEnabled.setEnabled(false);

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        boolean enable = check1.isSelected() && check3.isSelected();
        buttonToBeEnabled.setEnabled(enable);
    }
};

check1.addActionListener(actionListener);
check2.addActionListener(actionListener);
check3.addActionListener(actionListener);

这意味着:如果同时选择check1check3,则必须启用该按钮,否则必须禁用该按钮。当然,只有您知道应该选择哪些复选框组合才能设置按钮。

查看How to Use Buttons, Check Boxes, and Radio Buttons教程。

答案 1 :(得分:0)

建议可能是从您正在使用的每个组件派生一个类,并添加一个ActionListener,它会冒泡Container树并查找实现自定义界面的第一个Container,如下所示:

public interface MyCommandProcessor {
  void execute(String actionCommand);
}

public class MyButton extends JButton {
  public MyButton(string actionCommand) {
    setActionCommand(actionCommand);
    addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent ae) {
        Container traverser = MyButton.this;
        while (traverser != null && !(traverser instanceof MyCommandProcessor))
          traverser = traverser.getParent();
        if (traverser != null)
          ((CommandListener)traverser).execute(ae.getActionCommand());
      }
    });
  }
}

public class MyApp extends JFrame implements MyCommandListener {
  public MyApp() {
    JPanel panel = new Panel();
    panel.add(new MyButton("MyButton got pressed"));
  }

  public void execute(String actionCommand) {
    System.out.println(actionCommand);
  }
}

答案 2 :(得分:0)

您需要创建自定义组件侦听器。看这里:

Create a custom event in Java

Creating Custom Listeners In Java

http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html

我这样做会抛出标准的ActionListener 实施例

public class MyPanel extends JPanel {
  private final  JComboBox<String> combo1;
  private final JButton btn2;

  .......
   //catch the actions of inside components
   btn2.addActionListener(new MyPanelComponentsActionListener());
  ........

   //assign actionlistener to panel class
    public void addActionListener(ActionListener l) {
        listenerList.add(ActionListener.class, l);
    }
    public void removeActionListener(ActionListener l) {
        listenerList.remove(ActionListener.class, l);
    }

    //handle registered listeners from components used MyPanel class
   protected void fireActionPerformed(ActionEvent event) {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    ActionEvent e = null;
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
        if (listeners[i]==ActionListener.class) {
            // Lazily create the event:
            if (e == null) {
                  String actionCommand = event.getActionCommand();
                  if(actionCommand == null) {
                     actionCommand = "FontChanged";
                  }
                  e = new ActionEvent(FontChooserPanel.this,
                                      ActionEvent.ACTION_PERFORMED,
                                      actionCommand,
                                      event.getWhen(),
                                      event.getModifiers());
             }
                 // here registered listener executing
                ((ActionListener)listeners[i+1]).actionPerformed(e); 
            }
        }
    }

//!!! here your event generator
    class MyPanelComponentsActionListener implements ActionListener  {
     public void actionPerformed(ActionEvent e) {
        //do something usefull
        //.....
        fireActionPerformed(e); 
      }
     }
....
}