2015-12-02 54 views
0

所以這段代碼是來自我爲我的工作建立的應用程序。一個窗口獲取用戶名,然後他們選擇一個類別,然後他們參加測驗。中等尺寸測驗應用程序,問題動態生成標籤和RadioButtons

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和標籤。標籤按預期發佈,但嵌套循環應爲每個標籤創建一組RadioButton。我只用兩種不同的類別進行了測試,第一種是創建標籤,然後創建RadioButtons,但僅用於第一個標籤,並且創建了所有其他標籤。第二類創建標籤,因爲它應該但RadioButtons是真正的wonky,兩個來自第二個標籤,另外兩個在結尾。

First Category GUI

任何提示將非常理解的。

+0

爲每個組創建'JPanel',將標籤和單選按鈕放在它上面,將此面板添加到框架 – MadProgrammer

+0

,但是我的循環沒有問題,如果我無法獲得第一個標籤和廣播按鈕在一起? –

+0

如果有的話,我無法從您提供的不在上下文中的代碼片段中挑選出來。我傾向於不使用「BoxLayout」,所以我不確定這是否是問題。 – MadProgrammer

回答

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); 
} 

邏輯在上面的關於我們如何來到這個解決方案的評論中列出。