繁体   English   中英

从JRadioButton获取不属于ButtonGroup的文本

[英]Get text from JRadioButton that isn't part of a ButtonGroup

我的程序是一个GUI。 我有这种方法,当点击一个按钮时。 它动态地用JRadioButtons填充下一个屏幕。

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中被点击的文本。 现在我运行程序并选择一个按钮。 什么都没发生。

将按钮放入List<JToggleButton>例如ArrayList<JToggleButton> ,然后在需要信息时遍历列表。

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

请注意,JToggleButton是JRadioButton的父类,使用它可以将JRadioButtons,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或JCheckBox的代码,设置它们的actionCommand String,将它们放在GUI中,并将它们添加到ArrayList中。

部分(B)代码,增强的for循环需要在响应事件而调用的代码中,可能在JButton或单选按钮的ActionListener中。

请查看此信息并填写详细信息。 请考虑为我们创建和发布说明您的问题的sscce


编辑2
你的代码令人困惑,你似乎有两个完全不同类型的变量,名称完全相同,你似乎假设这将给出变量的神奇属性,让它知道它是“双胞胎”可能在做什么。 Java不能以这种方式工作,实际上变量名称几乎不是那么重要或聪明,以允许它们任何这样的功能。 相反,您的代码必须是智能的。 我假设将检查多个JCheckBox,并且您要检查在程序中的某个点检查哪些JCheckBox。 如果是这样,那么在你的类中你应该有一个List或ArrayList字段,比如

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

这样,该字段将在整个班级中可用。

然后在创建JCheckBox的位置,将它们添加到此列表中:

  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添加到按钮中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM