2011-07-30 10 views
8

मुद्रित करें मैं क्या चाहता हूं: बटन समूह में निहित जेआरडीओबटन का चयन करने के लिए आग लगने वाली एक घटना बनाएं, और फिर टेक्स्ट को प्रिंट करें JRadioButton पर ।"बच्चे" परिवर्तनों के लिए बटन ग्रुप पर सुनना, और चयनित जेआरडीओ बटन के पाठ

+0

कृपया 1 संपादित करें देखें। –

उत्तर

11

मेरी टिप्पणी के अनुसार, आप बटन समूह में श्रोता नहीं जोड़ सकते हैं। आपको व्यक्तिगत JRadioButtons में जोड़े गए एक्शनलिस्टर के साथ जाना होगा।

यदि यह आपके प्रश्न का उत्तर नहीं देता है, तो कृपया हमें अपनी समस्या के बारे में अधिक जानकारी बताएं।

संपादित करें 1
मैं तुम्हें हमेशा ButtonGroup का विस्तार कर सकता है ऐसी है कि वह ActionListeners स्वीकार करता है लगता है।

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

और निम्नलिखित द्वारा परीक्षण किया: उदाहरण के लिए:

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

लेकिन यह अभी भी अंत में प्रत्येक JRadioButton को ActionListers कहते हैं इसके समाप्त होता है को प्राप्त करने के लिए; यह सिर्फ MyButtonGroup की ऐड विधि ओवरराइड के दृश्यों के पीछे करता है।

संबंधित मुद्दे