2011-07-30 38 views

Trả lời

11

Theo nhận xét của tôi, bạn không thể thêm người nghe vào ButtonGroup. Bạn có thể sẽ phải đi với một ActionListener thêm vào JRadioButtons cá nhân.

Nếu điều này không trả lời được câu hỏi của bạn, vui lòng cho chúng tôi biết thêm chi tiết về sự cố của bạn.

Sửa 1
Tôi cho rằng bạn luôn có thể mở rộng ButtonGroup như vậy mà nó chấp nhận ActionListeners. Ví dụ:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.AbstractButton; 
import javax.swing.ButtonGroup; 
import javax.swing.ButtonModel; 
import javax.swing.event.EventListenerList; 

@SuppressWarnings("serial") 
public class MyButtonGroup extends ButtonGroup { 
    private ActionListener btnGrpListener = new BtnGrpListener(); 
    private EventListenerList listenerList = new EventListenerList(); 

    @Override 
    public void add(AbstractButton b) { 
     b.addActionListener(btnGrpListener); 
     super.add(b); 
    } 

    public void addActionListener(ActionListener listener) { 
     listenerList.add(ActionListener.class, listener); 
    } 

    public void removeActionListener(ActionListener listener) { 
     listenerList.remove(ActionListener.class, listener); 
    } 

    protected void fireActionListeners() { 
     Object[] listeners = listenerList.getListenerList(); 
     String actionCommand = ""; 
     ButtonModel model = getSelection(); 
     if (model != null) { 
     actionCommand = model.getActionCommand(); 
     } 
     ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand); 
     for (int i = listeners.length-2; i>=0; i-=2) { 
      if (listeners[i]== ActionListener.class) { 
       ((ActionListener)listeners[i+1]).actionPerformed(ae); 
      } 
     } 
    } 

    private class BtnGrpListener implements ActionListener { 

     public void actionPerformed(ActionEvent ae) { 
     fireActionListeners(); 
     } 
    } 
} 

Và thử nghiệm bởi những điều sau đây:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class MyButtonGroupTest { 
    private static void createAndShowUI() { 
     String[] data = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 

     JPanel panel = new JPanel(new GridLayout(0, 1)); 
     MyButtonGroup myBtnGrp = new MyButtonGroup(); 
     myBtnGrp.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Action Command is: " + e.getActionCommand()); 
     } 
     }); 

     for (String text : data) { 
     JRadioButton radioBtn = new JRadioButton(text); 
     radioBtn.setActionCommand(text); 
     myBtnGrp.add(radioBtn); 
     panel.add(radioBtn); 
     } 

     JFrame frame = new JFrame("MyButtonGroupTest"); 
     frame.getContentPane().add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowUI(); 
     } 
     }); 
    } 
} 

Nhưng điều này vẫn còn tiến thêm ActionListers cho mỗi JRadioButton để đạt được mục đích của nó; nó chỉ làm điều này đằng sau hậu trường trong ghi đè phương thức thêm của MyButtonGroup.

Các vấn đề liên quan