JPanel动作侦听器

我有一个带有一堆不同复选框和文本字段的JPanel,我有一个禁用的按钮,需要在设置特定配置时启用。 我需要的是整个JPanel上的监听器,只要有任何变化,就会查找事件。 我相信我需要一个动作监听器,但我找不到任何东西来将监听器与JPanel联系起来

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

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

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

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

你是完全正确的,这根本不是一个好习惯(参见DRY原则 )。 而不是你可以只定义一个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 ,则必须启用该按钮,否则必须禁用该按钮。 当然,只有您知道应该选择哪些复选框组合才能设置按钮。

看看如何使用按钮,复选框和单选按钮教程。

建议可以是从您正在使用的每个组件派生一个类,并添加一个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); } } 

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

用Java创建自定义事件

用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 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); } } .... }