2013-07-18 129 views
0

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

這只是一段代碼,我還沒有包括完整的代碼。

+0

如果我正確理解你的問題,你希望所有的單選按鈕都被取消選擇,對嗎? 在這種情況下,您必須致電: optionSelect.clearSelection();您的「清除」按鈕動作偵聽器中有 。 –

+0

是,並使用clearSelection();感謝你們的幫助 – usrNotFound

回答

1

使用ButtonGroup.clearSelection()

+0

是的,但我的單選按鈕在構造函數中,活動監聽器在不同的類 – usrNotFound

+0

什麼阻止你在類中的ButtonGroup optionSelect'字段,並讓你的動作偵聽器調用一個方法,調用'optionSelect.clearSelection ()'? – kiheru

+0

感謝隊友我把它全部整理出來。謝謝你的幫助 – usrNotFound