English 中文(简体)
如果按下 JButton “ 下一步” 键, 将出现另一个数学测验 [关闭]
原标题:Make another math quiz appear if a JButton "Next" is pressed [closed]

我正在使用 GUI 进行数学Quiz 程序。 这是我的代码, 现在我的问题是:

  1. How to make another question appear if the JButton Next is pressed.
  2. How to make the Next button work if and only if the answer is correct? (means that they must answer correctly before move to next question).

来人帮帮我,谢谢

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);
    }
}
问题回答

为解决问题,请使用 < a href=> http://docs.oracle.com/javase/tuative/uiswing/layout/card.html" rel="no follow"\\ code>CardLayout 。为解决问题,请使用简单的 if 语句。

  1. 而不是使用四个 JLabel, 您可以只使用一个, 当答案正确且您想要显示另一个问题时, 请在 JLabel 上调用 setText () 方法 。

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

  2. 当您创建 JButton 禁用它时 。

禁用创建时的按钮 。

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

在您的“行动倾听器”中的确认按钮

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

您还需要在“ 下一步” 按钮中添加一个“ 行动倾听器” 。

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
    }
  });




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签