我写了这段代码来从数据库中加载一些字符串。我想将这些字符串设置为我的 GUI 中的复选框。此表单有 2 个按钮,分别命名为 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);
}
}