随着我的小程序的进展,我有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:我已经让方法的所有代码,但如果需要,我可以删除不完全必要的部分。
答案 0 :(得分:3)
您所寻找的内容实际上称为 代码重构 ,因此您需要搜索如何重构代码?
您需要refactor
您的代码并编写单一责任类和更小的方法,以便更好地可理解/可读/可支持。
首先,您可以将所有MouseListener
实施到单独的类中,如下所示:
public class ButtonAMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonA MouseListener code here
}
public class ButtonBMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonB MouseListener code here
}
public class ButtonCMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonC MouseListener code here
}
public class ButtonDMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonD MouseListener code here
}
现在,为这些类创建对象并在原始类中使用它们。
答案 1 :(得分:1)
减少代码的最佳方法是将组件分组到数组中,这样就可以使用for来执行相同的任务(添加,命名,创建,添加侦听器),当然这并不总是建议或可能的,因为UI可以有很多不同类型的按钮,在你的情况下可以将按钮分组在一个数组中,我也会使用actionsPerformed而不是MouseEvent
JButton jButtons[] = new javax.swing.JButton[4];
for(int i=0;i<4;i++){
jButtons[i] = new javax.swing.JButton();
jButtons[i].setText(listeRep.get(i).getReponse());
jButtons[i].setActionCommand(""+i);
jButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Pressed");
switch (e.getActionCommand()) {
case "0":
//response A
break;
case "1":
//reponse B
break;
case "2":
//response C
break;
case "3":
//Response D
break;
default:
break;
}
});
getContentPane().add(jButtons[i]);
jButtons[i].setBounds(425, 325+i*75, 150, 75);
}
答案 2 :(得分:0)
我尝试了这个解决方案:
JUSTIFY
但它向我显示有关扩展和实现的错误,但考虑到你所说的我找到了解决方案:
我创建了:
public class ButtonAMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonA MouseListener code here
}
public class ButtonBMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonB MouseListener code here
}
public class ButtonCMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonC MouseListener code here
}
public class ButtonDMouseListener implements java.awt.event.MouseAdapter {
//implement ButtonD MouseListener code here
}
并且我可以像这样对4个按钮提出上诉:
private void initButon(javax.swing.JButton jButtonInited, Reponse reponse, Quizz quizz) {
jButtonInited.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (reponse.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);
}
}
});
}
它工作正常,我想我有其他方法需要再次感谢。