4

随着我的小程序的进展,我有 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:我已经让该方法的所有代码,但如果需要我可以删除不完全必要的部分。

4

2 回答 2

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
}

现在,为这些类创建对象并在原始类中使用它们。

于 2016-10-29T19:59:12.087 回答
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);
}
于 2016-10-29T20:22:59.197 回答