我试图在按下清除按钮时取消选择JRadio按钮。我试过Google搜索,并通过很多论坛唯一的解决办法,我可以找到创建一个不可见的按钮,而按下清除按钮时选择不可见的按钮。有没有其他方法可以使用?我的代码如下Java单选按钮清除/取消选择
public class deselectRadioBtn extends JFrame {
private JRadioButton[] buttons; // array for JRadio buttons
public deselectRadioBtn() {
super("Deselect Radio");
for (int nbrOfButtons = 0; nbrOfButtons < options.length; nbrOfButtons++) {
//create new JRadioButtons and labels and add () around label
buttons[nbrOfButtons] = new JRadioButton((nbrOfButtons + radioLabel[nbrOfButtons]));
//add buttons to eastPanel
rightPanel.add(options[nbrOfButtons]);
}//end for (JRadio)
//Create a ButtonGroup object, add buttons to the group
ButtonGroup optionSelect = new ButtonGroup();
optionSelect.add(buttons[0]);
optionSelect.add(buttons[1]);
optionSelect.add(buttons[2]);
optionSelect.add(buttons[3]);
}
这只是一段代码,我还没有包括完整的代码。
如果我正确理解你的问题,你希望所有的单选按钮都被取消选择,对吗? 在这种情况下,您必须致电: optionSelect.clearSelection();您的“清除”按钮动作侦听器中有 。 –
是,并使用clearSelection();感谢你们的帮助 – usrNotFound