我正在使用 GUI 进行数学Quiz 程序。 这是我的代码, 现在我的问题是:
- How to make another question appear if the
JButton
Next is pressed. - 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);
}
}