Java,使用一个ActionListener用于多个单选按钮

时间:2010-12-23 15:02:47

标签: java actionlistener

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

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

提前感谢任何提示。

John Doner

2 个答案:

答案 0 :(得分:5)

此代码将循环并以编程方式添加列表器。

    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();
                }
            }
        }
    }

答案 1 :(得分:3)

单选按钮正在吞下这个事件,它永远不会影响到JPanel。如果您想知道按下了哪个按钮,则需要为每个按钮添加动作侦听器。看看使用的踪迹 radio buttons