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、付费专栏及课程。

余额充值