【问题标题】:Java, using one ActionListener for multiple radio buttonsJava,对多个单选按钮使用一个 ActionListener
【发布时间】:2014-07-02 21:24:52
【问题描述】:

我在一个面板中有六个单选按钮,我想监听面板上的鼠标点击,然后确定选择了哪个单选按钮,并执行相应的操作。

但是当我设置这种情况并尝试它时,在动作监听器中有一个断点,代码似乎根本没有调用动作监听器。任何关于为什么会这样的解释,或者避免为每个按钮编写动作侦听器的替代方法,将不胜感激。

提前感谢您的任何提示。

约翰·多纳

【问题讨论】:

  • 一些代码会很好。

标签: java actionlistener


【解决方案1】:

此代码将循环并以编程方式添加listneers。

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;

    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;

    public class Test {
        public Test() {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            ButtonGroup bg = new ButtonGroup();

            for (int i = 0; i < 6; i++) {
                JRadioButton rb = new JRadioButton();
                // ID of Button
                rb.setActionCommand("button " + i);

                try {
                    //method to call, after pressed a button
                    Method m = getClass()
                            .getDeclaredMethod("RadioButton" + (i+1), null);
                    ActionListener al = new MyActionListener(m, this);
                    rb.addActionListener(al);
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //              
                bg.add(rb);
                panel.add(rb);
            }

            frame.setContentPane(panel);
            frame.setVisible(true);
            frame.pack();
        }
        /*buttons actions*/
        public void RadioButton1() {
            System.out.println("Boton1");
        }

        public void RadioButton2() {
            System.out.println("Boton2");
        }

        public void RadioButton3() {
            System.out.println("Boton3");
        }

        public void RadioButton4() {
            System.out.println("Boton4");
        }

        public void RadioButton5() {
            System.out.println("Boton5");
        }

        public void RadioButton6() {
            System.out.println("Boton6");
        }

        public static void main(String[] args) {
            new Test();
        }

        class MyActionListener implements ActionListener {
            Method call = null;
            Object object;

            public MyActionListener(Method m, Object value) {
                call = m;
                object = value;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //call method
                    call.invoke(object, null);
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    单选按钮正在吞噬该事件,并且它永远不会弥补 JPanel。如果您想知道按下了哪个按钮,则需要将动作侦听器添加到每个按钮。看看使用路径 radio buttons

    【讨论】:

    • 特别是鼠标事件的特殊之处在于,当且仅当子组件上没有侦听器时,它们才会冒泡到父容器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    相关资源
    最近更新 更多