我已将单选按钮添加到按钮组,但问题是可以同时选择两个单选按钮!我不知道该怎么做。请帮忙。 这是我当前代码的样子:
JRadioButton rEncrypt, rDecrypt;
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rEncrypt); bgroup.add(rDecrypt);
rEncrypt = new JRadioButton("Encryption");
rEncrypt.setBackground(bgColor);
rEncrypt.setSelected(true);
pEncrypt = new JPanel();
pEncrypt.setBackground(bgColor);
pEncrypt.add(rEncrypt);
rDecrypt = new JRadioButton("Decryption");
rDecrypt.setBackground(bgColor);
pDecrypt = new JPanel();
pDecrypt.setBackground(bgColor);
pDecrypt.add(rDecrypt);
答案 0 :(得分:2)
没有看到完全可运行的例子......
这样做......
JRadioButton rEncrypt, rDecrypt;
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rEncrypt); bgroup.add(rDecrypt);
之前...
rEncrypt = new JRadioButton("Encryption");
//...
rDecrypt = new JRadioButton("Decryption");
错了......
在将按钮添加到ButtonGroup
...