2010-12-23 58 views
4

我在面板中有六個單選按鈕,並且我想要在面板上單擊鼠標,然後確定選中哪個單選按鈕,並相應地執行操作。Java,使用一個ActionListener作爲多個單選按鈕

但是,當我設置這種情況並嘗試它,在動作偵聽器中有一個斷點時,代碼似乎根本不會調用動作偵聽器。任何解釋爲什麼這是如此,或避免爲每個按鈕編寫動作監聽器的替代方法,將不勝感激。

在此先感謝您的任何提示。

約翰·多納

+5

一些代碼會很好。 – Fortega 2010-12-23 15:06:26

回答

2

單選按鈕吞嚥事件及其從來沒有補差的JPanel的。如果您想知道哪個按鈕被按下,您需要將動作偵聽器添加到每個按鈕。看看使用的線索 radio buttons

+0

具體的鼠標事件是獨特的,因爲它們在父子容器中冒泡,當且僅當在子組件上沒有偵聽器時。 – 2010-12-23 15:20:46

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