中型测验应用,故障动态生成标签&单选按钮

时间:2015-12-02 20:33:15

标签: java swing

所以这段代码来自我为我的工作而建立的应用程序。一个窗口获取用户的名字,然后他们选择一个类别,然后他们参加测验。

static void QuestionsWindow(){
    ProtemInservices.SetActiveQuestions();
    JFrame frame = new JFrame("Quiz for: " + ProtemInservices.testeeName);        
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<JLabel> questions = new Vector<JLabel>();
    Vector<ButtonGroup> questionsGroup = new Vector<ButtonGroup>();
    Vector<JRadioButton> choices = new Vector<JRadioButton>();
    Vector<String> choicesString = new Vector<String>();
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    int counter = 0;
    for(int i=0;i<ProtemInservices.ActiveQuestions.size();i++){
        choicesString = new Vector<String>();
        questions.add(new JLabel(ProtemInservices.ActiveQuestions.elementAt(i).GetQuestionText()));
        ProtemInservices.ActiveQuestions.elementAt(i).CopyChoices(choicesString);
        questionsGroup.add(new ButtonGroup());
        frame.add(questions.elementAt(i));
        for(int j=0;j<choicesString.size();j++,counter++){
            choices.add(new JRadioButton(choicesString.elementAt(j)));
            questionsGroup.elementAt(i).add(choices.elementAt(j));
            frame.add(choices.elementAt(counter));
        }
    }
    frame.setMinimumSize(new Dimension(1280, 1024));

    //Display the window.
    //frame.pack();
    frame.setVisible(true);
}

我的问题是动态创建RadioButtons和Labels。标签按预期出现,但嵌套循环应该为每个Label创建一组RadioButtons。我只测试了两个不同的类别,第一个创建标签,然后创建RadioButtons,但只为第一个标签和创建所有其他标签后。第二类创建标签,但RadioButtons真的很不稳定,两个在第二个标签之后,另外两个在最后。

First Category GUI

任何提示都将非常感激。

1 个答案:

答案 0 :(得分:0)

static void QuestionsWindow(){
    ProtemInservices.SetActiveQuestions();
    JFrame frame = new JFrame("Quiz for: " + ProtemInservices.testeeName);        
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<JLabel> questions = new Vector<JLabel>();
    Vector<ButtonGroup> questionsGroup = new Vector<ButtonGroup>();
    Vector<JRadioButton> choices = new Vector<JRadioButton>();
    Vector<String> choicesString = new Vector<String>();
    frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
    int counter = 0;
    for(int i=0;i<ProtemInservices.ActiveQuestions.size();i++){
        choicesString = new Vector<String>();
        questions.add(new JLabel(ProtemInservices.ActiveQuestions.elementAt(i).GetQuestionText()));
        ProtemInservices.ActiveQuestions.elementAt(i).CopyChoices(choicesString);
        questionsGroup.add(new ButtonGroup());
        frame.add(questions.elementAt(i));
        for(int j=0;j<choicesString.size();j++,counter++){
            choices.add(new JRadioButton(choicesString.elementAt(j)));
            questionsGroup.elementAt(i).add(choices.elementAt(counter));
            frame.add(choices.elementAt(counter));
        }
    }
    frame.setMinimumSize(new Dimension(1280, 1024));

    //Display the window.
    //frame.pack();
    frame.setVisible(true);
}

上面在关于我们如何找到这个解决方案的评论中阐述了逻辑。