2012-12-21 143 views

回答

1
package radiobuttongroup; 

import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.LinkedHashMap; 
import java.util.Map; 

import javax.swing.ButtonGroup; 
import javax.swing.ButtonModel; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 

public class RadioButtonGroup { 

    public static void main(String[] args) { 
    new RadioButtonGroup(); 
    } 

    private RadioButtonGroup() { 
    Container contentPane = frame.getContentPane(); 
    contentPane.setLayout(new GridLayout(0, 1)); 

    for (int i = 1; i <= 5; i++) { 
     JRadioButton radio = new JRadioButton(Integer.toString(i)); 
     ButtonModel buttonModel = radio.getModel(); 
     modelToRadioButton.put(buttonModel, radio); 
     buttonGroup.add(radio); 
     contentPane.add(radio); 
    } 

    JButton buttonTestSelection = new JButton("Selection Test"); 
    JButton buttonClearSelection = new JButton("Clear Selection"); 
    contentPane.add(buttonTestSelection); 
    contentPane.add(buttonClearSelection); 

    buttonTestSelection.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent arg0) { 
      ButtonModel buttonModel = buttonGroup.getSelection(); 

      if (buttonModel == null) { 
      System.out.println("No radio button is selected"); 
      } 
      else { 
      if (modelToRadioButton.containsKey(buttonModel)) { 
       JRadioButton b = modelToRadioButton.get(buttonModel); 
       System.out.println("You selected button " + b.getText()); 
      } 
      else { 
       System.err.println("Weird, unrecognised button model!"); 
      } 
      } 
     } 
     }); 

    buttonClearSelection.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
      buttonGroup.clearSelection(); 
     } 
     }); 

    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 

    private JFrame frame = new JFrame("RadioButtonGroup"); 
    private Map<ButtonModel, JRadioButton> modelToRadioButton = 
    new LinkedHashMap<ButtonModel, JRadioButton>(); 
    private ButtonGroup buttonGroup = new ButtonGroup(); 
} 
1

既然你只有5個單選按鈕,你可以手動檢查所有的 (假設單選按鈕命名爲收音機1,...,radio5):

boolean isRadio1, isRadio2, isRadio3, isRadio4, isRadio5 = false; 

if (radio1.isSelected() == true) isRadio1 = true; 
if (radio2.isSelected() == true) isRadio2 = true; 
if (radio3.isSelected() == true) isRadio3 = true; 
if (radio4.isSelected() == true) isRadio4 = true; 
if (radio5.isSelected() == true) isRadio5 = true; 

不是最優雅的解決方案,但它會做到這一點。

9
buttonGroup1.getSelection()==null 
+0

嗯..沒有什麼新比較[先前的答案](http://stackoverflow.com/a/13988395/203657),有嗎? – kleopatra

+1

我喜歡它。有用。 – fungusanthrax

0

當找到選定的單選按鈕時,您無法通過ButtonModel做很多事情。但是ButtonGroup確實有方法getElements(),它返回AbstractButton的Enumeration。這些AbstractButtons可以通過枚舉迭代中投將JradioButton ...

下面的方法將返回選定一個JRadioButton的ButtonGroup中傳遞給它,如果選擇了沒有返回null ...

private JRadioButton getSelectedRadioButton(ButtonGroup buttonGroup) { 

    Enumeration<AbstractButton> abstractButtons = buttonGroup.getElements(); 
    JRadioButton radioButton = null; 

    while (abstractButtons.hasMoreElements()) { 
     radioButton = (JRadioButton) abstractButtons.nextElement(); 
     if (radioButton.isSelected()) { 
      break; 
     } 
    } 
    return radioButton; 
} 

一旦你選擇了JRadioButton,就可以訪問它的屬性。假設你想要任何按鈕被選中的文本屬性...

String selectedRadioButtonText = getSelectedRadioButton(buttonGroup).getText(); 

我希望這可以幫助別人!

1

此外,對於您的問題,您可以使用ButtonGroup類的getSelected()方法確定按鈕組的單選按鈕中是否存在選擇,因爲如果沒有選擇任何選項,則返回null。

private boolean isSelection(ButtonGroup buttonGroup) { 
    return (buttonGroup.getSelected() != null); 
} 

我希望這可以幫助別人!

相關問題