-3

我正在使用 GUI 做一个数学测验程序。这是我的代码,我现在的问题是:

  1. JButton Next如果按下 ,如何使另一个问题出现。
  2. Next当且仅当答案正确时,如何使按钮起作用?(意味着他们必须正确回答才能进入下一个问题)。

请有人帮助我,谢谢。

public class MathQuiz extends JFrame{

    JButton jbtNum1, jbtNum2, jbtNum3, jbtNum4, jbtNum5, jbtNum6, jbtNum7, jbtNum8, jbtNum9, jbtNum10, jbtNum11, jbtNum12, jbtNum13, jbtNum14, jbtNum15, jbtNum16, jbtNum17, jbtNum18, jbtNum19, jbtNum20;
    JButton jbtConfirm;
    JButton jbtNext;
    JTextField jtfAnswer = new JTextField();
    int a = (int)((Math.random())*10);
    int b = (int)((Math.random())*10);

    public MathQuiz(){

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(4,4,1,1));

            p1.add(jbtNum1 = new JButton("10"));
            p1.add(jbtNum2 = new JButton("7"));
            p1.add(jbtNum3 = new JButton("2"));
            p1.add(jbtNum4 = new JButton("18"));
            p1.add(jbtNum5 = new JButton("6"));
            p1.add(jbtNum6 = new JButton("11"));
            p1.add(jbtNum7 = new JButton("14"));
            p1.add(jbtNum8 = new JButton("1"));
            p1.add(jbtNum9 = new JButton("9"));
            p1.add(jbtNum10 = new JButton("3"));
            p1.add(jbtNum11 = new JButton("16"));
            p1.add(jbtNum12 = new JButton("17"));
            p1.add(jbtNum13 = new JButton("8"));
            p1.add(jbtNum14 = new JButton("15"));
            p1.add(jbtNum15 = new JButton("13"));
            p1.add(jbtNum16 = new JButton("5"));
            p1.add(jbtNum17 = new JButton("20"));
            p1.add(jbtNum18 = new JButton("12"));
            p1.add(jbtNum19 = new JButton("19"));
            p1.add(jbtNum20 = new JButton("4"));

        JPanel p2 = new JPanel(new GridLayout(1,5));

        p2.add(new JLabel(" " + a));
        p2.add(new JLabel(" + "));
        p2.add(new JLabel(" " + b));
        p2.add(new JLabel(" = "));
        p2.add(jtfAnswer);
        jtfAnswer.setEditable(false);
        p2.add(jbtConfirm = new JButton("Confirm"));
        p2.add(jbtNext = new JButton("Next"));

        setLayout(new GridLayout(3,1,50,50));
        add(p1);
        add(p2);

        jbtNum1.addActionListener(new ListenToButton());
        jbtNum2.addActionListener(new ListenToButton());
        jbtNum3.addActionListener(new ListenToButton());
        jbtNum4.addActionListener(new ListenToButton());
        jbtNum5.addActionListener(new ListenToButton());
        jbtNum6.addActionListener(new ListenToButton());
        jbtNum7.addActionListener(new ListenToButton());
        jbtNum8.addActionListener(new ListenToButton());
        jbtNum9.addActionListener(new ListenToButton());
        jbtNum10.addActionListener(new ListenToButton());
        jbtNum11.addActionListener(new ListenToButton());
        jbtNum12.addActionListener(new ListenToButton());
        jbtNum13.addActionListener(new ListenToButton());
        jbtNum14.addActionListener(new ListenToButton());
        jbtNum15.addActionListener(new ListenToButton());
        jbtNum16.addActionListener(new ListenToButton());
        jbtNum17.addActionListener(new ListenToButton());
        jbtNum18.addActionListener(new ListenToButton());
        jbtNum19.addActionListener(new ListenToButton());
        jbtNum20.addActionListener(new ListenToButton());
        jbtConfirm.addActionListener(new ListenToConfirm());
    }

    class ListenToButton implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == jbtNum1)
                jtfAnswer.setText(jbtNum1.getText());
            else if(e.getSource() == jbtNum2)
                jtfAnswer.setText(jbtNum2.getText());
            else if(e.getSource() == jbtNum3)
                jtfAnswer.setText(jbtNum3.getText());
            else if(e.getSource() == jbtNum4)
                jtfAnswer.setText(jbtNum4.getText());
            else if(e.getSource() == jbtNum5)
                jtfAnswer.setText(jbtNum5.getText());
            else if(e.getSource() == jbtNum6)
                jtfAnswer.setText(jbtNum6.getText());
            else if(e.getSource() == jbtNum7)
                jtfAnswer.setText(jbtNum7.getText());
            else if(e.getSource() == jbtNum8)
                jtfAnswer.setText(jbtNum8.getText());
            else if(e.getSource() == jbtNum9)
                jtfAnswer.setText(jbtNum9.getText());
            else if(e.getSource() == jbtNum10)
                jtfAnswer.setText(jbtNum10.getText());
            else if(e.getSource() == jbtNum11)
                jtfAnswer.setText(jbtNum11.getText());
            else if(e.getSource() == jbtNum12)
                jtfAnswer.setText(jbtNum12.getText());
            else if(e.getSource() == jbtNum13)
                jtfAnswer.setText(jbtNum13.getText());
            else if(e.getSource() == jbtNum14)
                jtfAnswer.setText(jbtNum14.getText());
            else if(e.getSource() == jbtNum15)
                jtfAnswer.setText(jbtNum15.getText());
            else if(e.getSource() == jbtNum16)
                jtfAnswer.setText(jbtNum16.getText());
            else if(e.getSource() == jbtNum17)
                jtfAnswer.setText(jbtNum17.getText());
            else if(e.getSource() == jbtNum18)
                jtfAnswer.setText(jbtNum18.getText());
            else if(e.getSource() == jbtNum19)
                jtfAnswer.setText(jbtNum19.getText());
            else if(e.getSource() == jbtNum20)
                jtfAnswer.setText(jbtNum20.getText());
        }
    }

    class ListenToConfirm implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int answer = a + b;
            int guessAnswer = Integer.parseInt(jtfAnswer.getText());

            if(guessAnswer == answer)
                jtfAnswer.setText("Correct");
            else
                jtfAnswer.setText("Wrong! Try Again.");
        }
    }

    public static void main(String[] args){
        MathQuiz frame = new MathQuiz();
        frame.setTitle("Math Quiz");
        frame.setSize(700,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
4

2 回答 2

3

要解决您的问题 1,请使用CardLayout. 要解决您的问题 2,请使用简单的if语句。

于 2012-05-24T15:17:57.787 回答
3
  1. 当答案正确并且您希望显示不同的问题时,您可以只使用一个并调用 JLabel 上的 setText() 方法,而不是使用四个 JLabel。

    JLabel question = new JLabel("你的第一个问题");

  2. 创建 JButton 时禁用它。

创建时禁用按钮。

    jbtNext = new JButton("Next");
    jbtNext.setEnabled(false);// button is initially disabled

在确认按钮的 ActionListener 中

   if(guessAnswer == answer){
        jtfAnswer.setText("Correct"); 
        jbtNext.setEnabled(true);
   }else 
       jtfAnswer.setText("Wrong! Try Again."); 

您还需要将 ActionListener 添加到“下一步”按钮。

jbtNext.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
      question.setText("Your next question");// change the question text
      jbtNext.setEnabled(false);// disable again until the next correct answer
    }
  });
于 2012-05-24T17:55:49.310 回答