2013-09-05 92 views
0

我寫這個代碼用於從數據庫加載一些字符串。我想將這些字符串設置到我的GUI中的複選框。該表單有兩個名爲Next和Previous的按鈕。當我選擇2複選框,然後單擊下一個按鈕它將加載其他字符串集。現在,當我點擊上一個按鈕時,我想先選中具有選定狀態的複選框。這些複選框正在動態創建。想要創建對象只有一次

private void loadAnswers(JPanel jp, String qId) { 
    jp.removeAll(); 
    try { 
     List<Question> listQuestion = questionController.performSearch(qId); 
     ArrayList<String> answers = new ArrayList<>(); 
     for (Question question : listQuestion) { 
      if (question.getOpA() != null) { 
       answers.add("<html>" + question.getOpA().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpB() != null) { 
       answers.add("<html>" + question.getOpB().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpC() != null) { 
       answers.add("<html>" + question.getOpC().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpD() != null) { 
       answers.add("<html>" + question.getOpD().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpE() != null) { 
       answers.add("<html>" + question.getOpE().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpF() != null) { 
       answers.add("<html>" + question.getOpF().replace("\n", "<br>") + "</html>"); 
      } 
      if (question.getOpG() != null) { 
       answers.add("<html>" + question.getOpG().replace("\n", "<br>") + "</html>"); 
      } 
     } 
     JCheckBox[] chkBx = new JCheckBox[answers.size()]; 
     for (int i = 0; i < chkBx.length; i++) { 
      chkBx[i] = new JCheckBox(); 
      chkBx[i].setText(answers.get(i)); 
      jPanel1.add(chkBx[i]); 
     } 
     jp.repaint(); 
     jp.revalidate(); 
    } catch (RemoteException ex) { 
     Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (SQLException ex) { 
     Logger.getLogger(TestPane.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
+1

,問題是......? – acdcjunior

+1

@acdcjunior但沒有任何問題的答案是CardLayout – mKorbel

+2

@mKorbel CardLayout將罰款,如果沒有重新創建每個問題的複選框。如果有很多問題,並且他們很有活力,Cardlayout將不適合。 – StanislavL

回答

4

組織類文本框的存儲(最好存儲模型 - 文本框的值)。我會建議一個地圖。當你必須移動到下一頁時,瀏覽所有的複選框,並將值放入地圖中。返回頁面時,只需再次從地圖中提取值並設置複選框的狀態。

主要可能是答案的文字,如果它是唯一或問題領域的字符串(question.getOpE())

+0

單頁有多個答案。 – RSST

+0

如何爲此使用地圖。 – RSST