java单选框多选一_【JAVA】java单选框二选一的问题

这篇博客主要介绍了如何在Java中创建并设置单选框(JRadioButton)以实现二选一的功能。通过创建两个JRadioButton实例,并加入到ButtonGroup中,理论上应该只能选择一个选项。然而,博主在实际运行代码时发现两个选项都可以被选中,对此提出了疑问。
摘要由CSDN通过智能技术生成

privateJRadioButtongetJRadioButton1(){if(jRadioButton1==null){jRadioButton1=newJRadioButton();jRadioButton1.setSelected(true);jRadioButton1.setText("\u6309\u7f16\u53f7");...

private JRadioButton getJRadioButton1() {

if(jRadioButton1 == null) {

jRadioButton1 = new JRadioButton();

jRadioButton1.setSelected(true);

jRadioButton1.setText("\u6309 \u7f16 \u53f7");

jRadioButton1.setBounds(340, 115, 74, 23);

}

return jRadioButton1;

}

private JRadioButton getJRadioButton2() {

if(jRadioButton2 == null) {

jRadioButton2 = new JRadioButton();

jRadioButton2.setText("\u6309 \u7247 \u540d");

jRadioButton2.setBounds(340, 137, 74, 23);

}

return jRadioButton2;

}

private JLabel getJLabel2() {

if(jLabel2 == null) {

jLabel2 = new JLabel();

jLabel2.setText("\u63d0\u793a:");

jLabel2.setBounds(340, 442, 44, 15);

}

return jLabel2;

}

private JLabel getJLabel11() {

if(jLabel11 == null) {

jLabel11 = new JLabel();

jLabel11.setText("\u5982\u6240\u67e5\u8be2\u7684\u97f3\u50cf\u5df2\u501f\u51fa\uff0c\u70b9\u51fb\u7ee7\u7eed\uff0c\u6309\u59d3\u540d\u67e5\u627e\u3002");

jLabel11.setBounds(379, 442, 396, 15);

}

return jLabel11;

}

private void jButton2ActionPerformed(ActionEvent evt) {

System.out.println("jButton2.actionPerformed, event="+evt);

//TODO add your code for jButton2.actionPerformed

}

public static void main(String[] args) {

ButtonGroup group = new ButtonGroup ();

group.add(jRadioButton1);

group.add(jRadioButton2);

}

}

运行没有错误但是为什么还是两个选项都能选呢?

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值