我試圖在按下清除按鈕時取消選擇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