java如何用Jradiobutton做一个有选项的button组,然后获取选定的选项是哪个?

2024-11-20 21:39:55
推荐回答(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
//女被选处理