我寫這個代碼用於從數據庫加載一些字符串。我想將這些字符串設置到我的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);
}
}
,問題是......? – acdcjunior
@acdcjunior但沒有任何問題的答案是CardLayout – mKorbel
@mKorbel CardLayout將罰款,如果沒有重新創建每個問題的複選框。如果有很多問題,並且他們很有活力,Cardlayout將不適合。 – StanislavL