Java 将侦听器添加到JPanel中的所有对象

Java 将侦听器添加到JPanel中的所有对象,java,swing,jpanel,listeners,Java,Swing,Jpanel,Listeners,我有一个包含许多对象的JPanel,还有一个可以执行的主要操作:计算。有一个按钮可以执行此操作,还有一个JTextField和其他组件,用户可能希望在其中按enter键。例如,如果从JComboBox中选择某个对象并按enter键,则将进行计算。有没有一种简单的方法可以将这样的侦听器添加到JPanel的所有内容中,而不是将ActionListener添加到每个组件中?JPanel扩展了继承了容器的JComponent。您可以使用getComponents()。您将获得一个组件[]数组,您可以循环

我有一个包含许多对象的JPanel,还有一个可以执行的主要操作:计算。有一个按钮可以执行此操作,还有一个JTextField和其他组件,用户可能希望在其中按enter键。例如,如果从JComboBox中选择某个对象并按enter键,则将进行计算。有没有一种简单的方法可以将这样的侦听器添加到JPanel的所有内容中,而不是将ActionListener添加到每个组件中?

JPanel
扩展了继承了
容器的
JComponent
。您可以使用
getComponents()
。您将获得一个
组件[]
数组,您可以循环并为每个组件添加该数组,它是
组件
的子类,如
按钮
,并为每个组件添加相同的
ActionListener
。请参见

JPanel
扩展
JComponent
,它继承了
容器
。您可以使用
getComponents()
。您将获得一个
组件[]
数组,您可以循环并为每个组件添加该数组,它是
组件
的子类,如
按钮
,并为每个组件添加相同的
ActionListener
。见

@cinhtau有正确的方法。由于没有具有“addActionListener”方法的公共类型,这就变得更加困难了。您必须检查要为其添加操作侦听器的每个案例

public static void addActionListenerToAll( Component parent, ActionListener listener ) {
    // add this component
    if( parent instanceof AbstractButton ) {
        ((AbstractButton)parent).addActionListener( listener );
    }
    else if( parent instanceof JComboBox ) {
        ((JComboBox<?>)parent).addActionListener( listener );
    }
    // TODO, other components as needed

    if( parent instanceof Container ) {
        // recursively map child components
        Component[] comps = ( (Container) parent ).getComponents();
        for( Component c : comps ) {
            addActionListenerToAll( c, listener );
        }
    }
}
publicstaticvoid addActionListenerToAll(组件父级,ActionListener侦听器){
//添加此组件
if(抽象按钮的父实例){
((AbstractButton)parent.addActionListener(listener);
}
else if(JComboBox的父实例){
((JComboBox)父级).addActionListener(listener);
}
//TODO,其他需要的组件
if(容器的父实例){
//递归映射子组件
组件[]comps=((容器)父级).getComponents();
用于(组件c:组件){
addActionListenerToAll(c,listener);
}
}
}

@cinhtau有正确的方法。由于没有具有“addActionListener”方法的公共类型,这就变得更加困难了。您必须检查要为其添加操作侦听器的每个案例

public static void addActionListenerToAll( Component parent, ActionListener listener ) {
    // add this component
    if( parent instanceof AbstractButton ) {
        ((AbstractButton)parent).addActionListener( listener );
    }
    else if( parent instanceof JComboBox ) {
        ((JComboBox<?>)parent).addActionListener( listener );
    }
    // TODO, other components as needed

    if( parent instanceof Container ) {
        // recursively map child components
        Component[] comps = ( (Container) parent ).getComponents();
        for( Component c : comps ) {
            addActionListenerToAll( c, listener );
        }
    }
}
publicstaticvoid addActionListenerToAll(组件父级,ActionListener侦听器){
//添加此组件
if(抽象按钮的父实例){
((AbstractButton)parent.addActionListener(listener);
}
else if(JComboBox的父实例){
((JComboBox)父级).addActionListener(listener);
}
//TODO,其他需要的组件
if(容器的父实例){
//递归映射子组件
组件[]comps=((容器)父级).getComponents();
用于(组件c:组件){
addActionListenerToAll(c,listener);
}
}
}

我现在就是这么做的,而且效果很好

private void setActionListeners() {
        for (Component c : this.getComponents()){
            if (c.getClass() == JMenuItem.class){
                JMenuItem mi = (JMenuItem) c;
                mi.addActionListener(this);
            }
            if (c.getClass() == JCheckBoxMenuItem.class){
                JCheckBoxMenuItem cmi = (JCheckBoxMenuItem) c;
                cmi.addActionListener(this);
            }
        }
    }

这就是我现在所做的,它起了作用

private void setActionListeners() {
        for (Component c : this.getComponents()){
            if (c.getClass() == JMenuItem.class){
                JMenuItem mi = (JMenuItem) c;
                mi.addActionListener(this);
            }
            if (c.getClass() == JCheckBoxMenuItem.class){
                JCheckBoxMenuItem cmi = (JCheckBoxMenuItem) c;
                cmi.addActionListener(this);
            }
        }
    }

? ?您可能需要使用递归来实现这一点,因为组件很可能嵌套在容器中。@装满鳗鱼的气垫船您可能需要使用递归来实现这一点,因为组件很可能嵌套在容器中。@装满鳗鱼的气垫船