如何检查是否在buttongroup中选择了radiobutton?

时间:2012-12-21 08:12:38

标签: java swing netbeans-7

我有一组5个单选按钮,我需要检查是否至少选择了其中一个。

这是对的吗?

buttonGroup1.isSelected(ButtonModel);

但这里的ButtonmModel是什么?

5 个答案:

答案 0 :(得分:10)

buttonGroup1.getSelection()==null

答案 1 :(得分: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();
}

答案 2 :(得分:1)

由于您只有5个单选按钮,因此您可以手动检查所有按钮 (假设单选按钮名为radio1,...,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;

不是最优雅的解决方案,但它可以解决问题。

答案 3 :(得分:1)

此外,还有更多问题,您可以使用ButtonGroup类的getSelected()方法确定按钮组的单选按钮中是否有选择,因为如果未选择任何内容,则返回null。 / p>

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

我希望这有助于某人!!!

答案 4 :(得分:0)

在查找选定的单选按钮时,您无法通过ButtonModel做很多事情。但是ButtonGroup确实有getElements()方法,它返回AbstractButtons的Enumeration。这些AbstractButtons可以在迭代期间通过枚举...

强制转换为JRadioButtons

以下方法将返回传入其中的ButtonGroup的选定JRadioButton,如果没有选择则返回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后,它是访问其属性的基础。假设你想要选择任何按钮的text属性......

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

我希望这有助于某人!!!