随着我的小程序的进展,我有 4 个按钮,它们是相同规则的规则,我不知道如何减少代码,我几乎可以肯定它可以简化:
private void initGame(Quizz quizz) {
jLabelScore = new javax.swing.JLabel();
jLabelComptQ = new javax.swing.JLabel();
jButtonA = new javax.swing.JButton();
jButtonB = new javax.swing.JButton();
jButtonC = new javax.swing.JButton();
jButtonD = new javax.swing.JButton();
int i = quizz.aleatQuestion();
ArrayList<Reponse> listeRep = quizz.showAnswer(i);
Reponse reponseA = listeRep.get(0);
Reponse reponseB = listeRep.get(1);
Reponse reponseC = listeRep.get(2);
Reponse reponseD = listeRep.get(3);
jButtonA.setText(reponseA.getReponse());
jButtonB.setText(reponseB.getReponse());
jButtonC.setText(reponseC.getReponse());
jButtonD.setText(reponseD.getReponse());
jLabelScore.setText("Score : " + quizz.getScore());
int a = quizz.getCompteurQ()+1;
jLabelComptQ.setText("Question n°" + a);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Jeu en Cours - StudenTest");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setMaximumSize(new java.awt.Dimension(800, 600));
setMinimumSize(new java.awt.Dimension(800, 600));
setResizable(false);
setSize(new java.awt.Dimension(800, 600));
getContentPane().setLayout(null);
getContentPane().removeAll();
getContentPane().add(jLabelScore);
jLabelScore.setBounds(0, 20, 250, 250);
jLabelScore.setVerticalAlignment(SwingConstants.TOP);
getContentPane().add(jLabelComptQ);
jLabelComptQ.setBounds(0, 0, 250, 250);
jLabelComptQ.setVerticalAlignment(SwingConstants.TOP);
jButtonA.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (reponseA.getVerif()) {
quizz.win();
} else {
quizz.lose();
}
quizz.endOfTurn();
if (quizz.getCompteurQ() >= 5) {
Component frame = null;
JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
initComponents();
} else {
initGame(quizz);
}
}
});
getContentPane().add(jButtonA);
jButtonA.setBounds(425, 325, 150, 75);
jButtonB.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (reponseB.getVerif()) {
quizz.win();
} else {
quizz.lose();
}
quizz.endOfTurn();
if (quizz.getCompteurQ() >= 5) {
Component frame = null;
JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
initComponents();
} else {
initGame(quizz);
}
}
});
getContentPane().add(jButtonB);
jButtonB.setBounds(225, 325, 150, 75);
jButtonC.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (reponseC.getVerif()) {
quizz.win();
} else {
quizz.lose();
}
quizz.endOfTurn();
if (quizz.getCompteurQ() >= 5) {
Component frame = null;
JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
initComponents();
} else {
initGame(quizz);
}
}
});
getContentPane().add(jButtonC);
jButtonC.setBounds(425, 450, 150, 75);
jButtonD.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (reponseD.getVerif()) {
quizz.win();
} else {
quizz.lose();
}
quizz.endOfTurn();
if (quizz.getCompteurQ() >= 5) {
Component frame = null;
JOptionPane.showMessageDialog(frame, "Score Final :" + quizz.getScore() + "\nBien Joué");
initComponents();
} else {
initGame(quizz);
}
}
});
getContentPane().add(jButtonD);
jButtonD.setBounds(225, 450, 150, 75);
pack();
setLocationRelativeTo(null);
}
您可以看到 reponseA/B/C/D 和 jButtonA/B/C/D 遵循相同的指令。我复习了我的课程,但即使它对我有很大帮助,我也没有找到任何解决这个问题的方法。我试图进行在线研究,但它不相关或解决方案不适合......我希望你明白我所说的(我的英语不流利)。
Ps:我已经让该方法的所有代码,但如果需要我可以删除不完全必要的部分。