【问题标题】:Get text from JRadioButton that isn't part of a ButtonGroup从 JRadioButton 获取不属于 ButtonGroup 的文本
【发布时间】:2013-12-15 11:44:20
【问题描述】:

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

【问题讨论】:

  • 1) 请不要忘记添加“?”提问!有些人在页面中搜索“?”如果“问题”中不存在,则直接转到下一个(实际)问题。 2) 为了尽快获得更好的帮助,请发帖SSCCE

标签: java swing event-handling jradiobutton


【解决方案1】:

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

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 或 JCheckBoxes,设置它们的 actionCommand 字符串,将它们放置在 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 添加到按钮中

【讨论】:

  • ArrayList settingQuestionButton = new ArrayList(); for(JToggleButton 问题:settingQuestionButton) { if(questions.isSelected()) { System.out.println(questions.getActionCommand());这不起作用@Hovercraft Full Of Eels
  • @C-Elipse:“不工作”没有给我足够的信息来让我理解可能出了什么问题。请考虑编辑您的原始问题并将任何新的或更改的代码以及任何错误消息或新问题的描述添加到底部。不要删除旧代码。另外,请记住,在创建 JRadioButtons 和 JCheckBoxes 时,您通常必须显式设置 actionCommand 文本。
  • 我注意到的另一件事是 OP 实际上从未将任何单选按钮添加到列表中。让我感到困惑的另一件事是settingQuestionButton 变量的使用。 OP 问题的第一部分用作单选按钮对象,但在第二部分中,它也用作列表。
  • @C-Elipse:请重新阅读我的回答,因为您似乎没有遵循我的建议。如有任何不明白的地方,请要求澄清。
  • @Hovercraft Full Of Eels:当您说“您应该按照 (A) 的代码创建您的 JRadioButtons 或 JCheckBoxes,设置它们的 actionCommand 字符串,将它们放置在 GUI 中,然后添加它们到 ArrayList。”我对那部分感到困惑,我认为:settingQuestionBoxes = new JCheckBox(questions[j]) - 在我的代码的第一部分中已经这样做了。你能澄清一下吗?
猜你喜欢
  • 2017-05-09
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多