2013-12-14 21 views
1

我的程序是一個GUI。我有這種方法,當點擊一個按鈕。它使用JRadioButton動態地填充下一個屏幕。從不屬於ButtonGroup的JRadioButton獲取文本

private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt) 
{     
     if(evt.getActionCommand().equals("Set Exam")) 
     { 
      CardLayout cL = (CardLayout)cardPanels.getLayout(); 
      cL.show(cardPanels, "setExamPanel"); 
     } 

     try 
     { 
      //InputStream code 

      String theMessage = myObject.getMessage();   

      String delims = "(?=(0*([0-9]{1,2}|100)))"; 
      String[] questions = theMessage.split(delims); 

      System.out.println(Arrays.toString(questions));   

      for (int j = 1; j < questions.length; j++) 
      { 
       settingQuestionBoxes = new JCheckBox(questions[j]);    

       settingQuestionTextField = new JTextField(""); 

       jPanel1.add(settingQuestionBoxes);    
       jPanel1.add(settingQuestionTextField); 
       jPanel1.revalidate(); 
       jPanel1.repaint();     

      } 

      //close streams and socket code 

     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
} 

然後我從另一個屏幕上的其他方法,從上一個方法填充的數據轉到。

private void setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) 
    { 
      if(evt.getActionCommand().equals("Set Exam Question")) 
      {   
         ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>(); 

         for(JToggleButton questions: settingQuestionBoxes) 
         {    
          if(questions.isSelected()) 
          {    
           System.out.println(questions.getActionCommand()); 
          } 
         }   

         CardLayout cL = (CardLayout)cardPanels.getLayout(); 
         cL.show(cardPanels, "instructorPanel"); 
      }    
    } 

所以基本上,當我把這個的System.out.println(questions.getActionCommand())我想看到被點擊選擇JRadioButton的文本。 現在,當我運行程序並選擇一個按鈕。什麼都沒發生。

+0

1)請不要忘記添加'?'提問!有些人在頁面中搜索'?'如果'問題'中不存在,則直接進入下一個(實際)問題。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

3

將按鈕放入List<JToggleButton>(如ArrayList<JToggleButton>)中,然後在需要信息時遍歷列表。

for (JToggleButton btn : myButtonList) { 
    if (btn.isSelected() { 
    String actionCommand = btn.getActionCommand(); 
    // use the actionCommand here 
    } 
} 

注意JToggleButton中是一個JRadioButton父類,並用它允許你將JradioButton,JCheckBoxes和JToggleButtons添加到列表中。由於您的JRadioButton不是ButtonGroup的一部分,因此您可能應該使用JCheckBox。


編輯

現在,您已經張貼了這個代碼,說明它不工作:

// Section (A) 
ArrayList<JToggleButton> settingQuestionButton = new ArrayList<JToggleButton>(); 

// Section (B) 
for(JToggleButton questions: settingQuestionButon) 
{    
    if(questions.isSelected()) 
    {    
     System.out.println(questions.getActionCommand()); 
    } 
} 

這段代碼,無論是(A)和(B),一起在你的程序中?如果是這樣,這是不合理的。你應該有(A)在構造函數或者一些設置方法。你應該遵循(A)的代碼創建你的JRadioButtons或JCheckBoxes,它們設置了它們的actionCommand String,它們將它們放在GUI中,並將它們添加到ArrayList。

部分(B)代碼,增強for循環將需要在爲響應事件而調用的代碼中,可能是在JButton或單選按鈕的ActionListener中。

請查看此信息並填寫詳細信息。請考慮創建併發布sscce,以說明您對我們的問題。


編輯2
您的代碼混淆了,你似乎有兩種不同類型的完全變量具有完全相同的名稱,你似乎是假設這會給變量神奇性質將讓它知道它的「雙胞胎」可能在做什麼。 Java不能這樣工作,事實上變量名稱幾乎不是那麼重要或很聰明,以至於它們沒有任何這樣的功能。而你的代碼必須很聰明。 我假設你的JCheckBox中有多個JCheckBox會被檢查,並且你想檢查你的程序中某個點檢查了哪些。如果是這樣,那麼在你的類,你應該有一個列表或ArrayList的領域,像

private List<JToggleButton> questionsList = new ArrayList<JToggleButton>(); 

這樣,這個領域將提供整個班級。

然後在其中創建您的JCheckBoxes,將它們添加到這個列表:

private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {     
     if(evt.getActionCommand().equals("Set Exam")) 
     { 
      CardLayout cL = (CardLayout)cardPanels.getLayout(); 
      cL.show(cardPanels, "setExamPanel"); 
     } 

     try 
     { 
      String theMessage = myObject.getMessage();   

      String delims = "(?=(0*([0-9]{1,2}|100)))"; 
      String[] questions = theMessage.split(delims); 

      for (int j = 1; j < questions.length; j++) 
      { 
       settingQuestionBox = new JCheckBox(questions[j]); // *** renamed to make more sense 
       settingQuestionBox.setActionCommand(questions[j]); // **** add actionCommand String 
       questionsList.add(settingQuestionBox); // ****** add JCheckBox to List 

       settingQuestionTextField = new JTextField(""); 

       jPanel1.add(settingQuestionBox);    
       jPanel1.add(settingQuestionTextField); 
       jPanel1.revalidate(); 
       jPanel1.repaint();     

      } 

      //close streams and socket code 

     } 
     catch(Exception e) 
     { 
      // System.out.println(e); 
      e.printStackTrace(); // ***** more informative 
     } 
    } 

然後在其他地方在你的代碼

setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) 
    { 
    if(evt.getActionCommand().equals("Set Exam Question")) 
    {   
     // ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>(); 

     for(JToggleButton questions: questionsList) 
     {    
      if(questions.isSelected()) 
      {    
       System.out.println(questions.getActionCommand()); 
      } 
     }   

     CardLayout cL = (CardLayout)cardPanels.getLayout(); 
     cL.show(cardPanels, "instructorPanel"); 
     } 
    }  

當然,你需要照顧的的ActionListener被添加到按鈕

+0

噪聲刪除.. –

+0

ArrayList settingQuestionButton = new ArrayList (); 爲(JToggleButton中的問題:settingQuestionButton) { \t \t 如果(questions.isSelected()) \t \t {\t \t \t的System.out.println(questions.getActionCommand()); \t} } 這是行不通的@Hovercraft Full Of Eels –

+0

@ C-Elipse:「不工作」並沒有給我足夠的信息讓我明白什麼可能是錯的。請考慮編輯您的原始問題,並在底部添加任何新的或已更改的代碼以及任何錯誤消息或新問題的描述。不要刪除舊代碼。另外,請記住,在創建JRadioButtons和JCheckBoxes時,通常必須明確設置actionCommand文本。 –