Frage

Ich habe sechs Radio-Buttons in einem Panel, und ich möchte für einen Mausklick auf der Platte hören, dann bestimmen, welches Optionsfeld ausgewählt wird, und eine Aktion entsprechend durchführen.

Aber wenn ich diese Situation eingestellt und versuchte es mit einem Haltepunkt in der Aktion Hörer, der Code scheint nicht die Aktion Hörer überhaupt aufzurufen. Jede Erklärung, warum dies so ist, oder eine alternative Art und Weise Aktion Hörer für jede Taste zu vermeiden, schreiben, würde sehr geschätzt werden.

Vielen Dank im Voraus für alle Tipps.

John Doner

War es hilfreich?

Lösung

Die Radiotasten das Ereignis schlucken und sein es nie auf die JPanel bilden. Wenn Sie wissen möchten, welche Taste gedrückt wurde, müssen Sie die Aktion Hörer zu jeder der Tasten hinzuzufügen. Werfen Sie einen Blick auf den Spuren auf der Verwendung Radiobuttons

Andere Tipps

Dieser Code wird Schleife und programmatisch die listneers hinzuzufügen.

    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();
                }
            }
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top