简体   繁体   English

如果选中另一个,我该如何取消选中radiobutton?

[英]How do I uncheck radiobutton if another is checked?

I am using a content pane with gridbag layout and i have created 3 JRadioButtons . 我正在使用带有gridbag布局的内容窗格,我创建了3个JRadioButtons I have an actionlistener that makes a command when they are press and in a different method I handle the actions. 我有一个actionlistener ,它在按下时发出命令,在我处理动作的其他方法中。

I tried buttonname.setSelected(false); 我试过了buttonname.setSelected(false); and it does not work. 它不起作用。 I searched about this and people were talking about button groups so I created one and added the buttons to the group like this: 我搜索了这个,人们正在谈论按钮组,所以我创建了一个,并将按钮添加到组中,如下所示:

    ButtonGroup group = new ButtonGroup();
    group.add(color1);
    group.add(color2);
    group.add(color3);

I put that in my pane thing where I add components to pane after I added the buttons with pane.add(buttonname, c); 我把它放在我的窗格中,在我用pane.add(buttonname, c);添加按钮后,我将组件添加到窗格pane.add(buttonname, c);

How do I group.clearSelection() or whatever the command is from my action handling method? 我如何group.clearSelection()或任何来自我的动作处理方法的命令?

This is from the Javadocs of ButtonGroup 这是来自ButtonGroup的Javadocs

This class is used to create a multiple-exclusion scope for a set of buttons. 此类用于为一组按钮创建多重排除范围。 Creating a set of buttons with the same ButtonGroup object means that turning "on" one of those buttons turns off all other buttons in the group. 使用相同的ButtonGroup对象创建一组按钮意味着“打开”其中一个按钮会关闭组中的所有其他按钮。 A ButtonGroup can be used with any set of objects that inherit from AbstractButton. ButtonGroup可以与从AbstractButton继承的任何对象集一起使用。

So it would seem that you do not need to do anything. 所以看起来你不需要做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM