Java t(); } //关闭流和套接字代码 } 捕获(例外e) { //系统输出打印ln(e); e、 printStackTrace();//****更多信息 } }

Java t(); } //关闭流和套接字代码 } 捕获(例外e) { //系统输出打印ln(e); e、 printStackTrace();//****更多信息 } },java,swing,event-handling,jradiobutton,Java,Swing,Event Handling,Jradiobutton,然后在代码的其他地方 setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) { if(evt.getActionCommand().equals("Set Exam Question")) { // ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JTog

然后在代码的其他地方

  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");
      }
  }    
setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals(“设置考试问题”))
{           
//ArrayList SettingQuestionBox=新建ArrayList();
用于(JToggleButton问题:问题列表)
{               
if(questions.isSelected())
{               
System.out.println(questions.getActionCommand());
}
}           
CardLayout cL=(CardLayout)cardPanels.getLayout();
cL.show(cardPanels,“instructorPanel”);
}
}    

当然,您需要注意将ActionListener添加到按钮中(请不要忘记在问题中添加“?”)!有些人在页面中搜索“?”,如果“问题”中不存在,则直接转到行中的下一个(实际)问题。2) 为了更快地获得更好的帮助,发布一个.ArrayList settingQuestionButton=new ArrayList();对于(JToggleButton问题:settingQuestionButton){if(questions.isSelected()){System.out.println(questions.getActionCommand());}}}这在充满Eels@C-Elipse:“不起作用”并没有给我足够的信息让我理解可能的错误。请考虑编辑您原来的问题,并添加任何新的或更改的代码到底部,以及任何错误信息或新问题的描述。不要删除旧代码。另外,请记住,在创建JRadioButtons和jcheckbox时,通常必须显式设置actionCommand文本。我注意到的另一件事是,OP实际上从未向列表中添加任何单选按钮。另一件让我困惑的事情是
settingQuestionButton
变量的用法。OP问题的第一部分用作单选按钮对象,但在第二部分,它也用作列表。@C-Elipse:请重新阅读我的答案,因为您似乎没有遵循我的建议。请澄清您不理解的任何内容。@Hovercraft Full of EEL:当您说“您应该遵循(A)的代码来创建JRadioButtons或JCheckbox,设置它们的actionCommand字符串,将它们放在GUI中,并将它们添加到ArrayList中。”我对这一部分感到困惑,我认为:settingquestionbox=newjcheckbox(questions[j])——在我的代码的第一部分中已经做到了这一点。你能澄清一下吗。
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");
             }              
     }
for (JToggleButton btn : myButtonList) {
   if (btn.isSelected() {
     String actionCommand = btn.getActionCommand();
     // use the actionCommand here
   }
}
// Section (A)
ArrayList<JToggleButton> settingQuestionButton = new ArrayList<JToggleButton>();

// Section (B)
for(JToggleButton questions: settingQuestionButon)  
{               
    if(questions.isSelected())
    {               
        System.out.println(questions.getActionCommand());
    }
}
private List<JToggleButton> questionsList = new ArrayList<JToggleButton>();
  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");
      }
  }