JAVA中JRadioButton控件怎么样获取选择值

2024-11-16 10:38:23
推荐回答(1个)
回答1:

//声名两个单选按钮
private JRadioButton male;
private JRadioButton female;
//初始化按钮,男为默认选中
male = new JRadioButton("男", true);
female = new JRadioButton("女");
//将两个按钮放到同一个组中
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
//在actionPerformed(...)中获得按钮的选择值
DefaultButtonModel model = (DefaultButtonModel) male.getModel();
if (model.getGroup().isSelected(model))
//男被选中的处理

else
//女被选中的处理