6

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

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

提前感谢您的任何提示。

约翰·多纳

4

2 回答 2

5

此代码将循环并以编程方式添加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();
                }
            }
        }
    }
于 2010-12-23T16:00:23.890 回答
4

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

于 2010-12-23T15:12:49.977 回答