在我的代码中,当球反弹到墙壁上时,弹出的Jframe会继续弹出。我希望球碰到墙壁时不再弹出。只有正确回答了jframe提供的问题中的答案,球才会继续。
我尝试在QandA.hor中使用循环,以便它只会在输入答案之前调用保持循环。
以下是一些代码:
public Random r = new Random();
public menu m;
public String[] questions= {"what is 1+1?","What is the product of 3 and 6?","what is the difference between 300 and 150?",
"how many 5 apples in a cartel of 50 apples?"};
public String[] answers = {"2","18","150","10"};
public Ball b;
public JTextField qna;
public int num = r.nextInt(3);
public void hor () {
JFrame f = new JFrame();
f.setSize(600, 400);
f.setVisible(true);
f.setLocationRelativeTo(null);
JLabel qn = new JLabel(questions[num]);
qn.setBounds(100,50,400,100);
f.add(qn);
JTextField qna = new JTextField("");
qna.setBounds(100,150, 200, 50);
f.add(qna);
qna.addKeyListener(this);
}
public void ver () {
JFrame f = new JFrame();
f.setSize(600,400);
f.setVisible(true);
f.setLocationRelativeTo(null);
int num = r.nextInt(3);
JLabel q1 = new JLabel(questions[num]);
q1.setBounds(100,50,400,100);
f.add(q1);
JTextField qna = new JTextField("");
qna.setBounds(100,150, 200, 50);
f.add(qna);
qna.addKeyListener(this);
}
有公共无效键已按下
// TODO Auto-generated method stub
int key = e.getKeyCode();
String ans1 = qna.getText();
if(key == KeyEvent.VK_ENTER) {
if (ans1.equalsIgnoreCase(answers[num])) {
Ball.setValue = true;
}
}
}
其中Ball.java
public int x,y;
public double velX=2, velY=2;
public boolean bw = true;
public bounce anim;
public Ball ball;
QandA qna = new QandA();
Random r = new Random();
Timer t = new Timer(10, this);
public Ball(bounce anim) {
x = anim.r.nextInt(940);
y = anim.r.nextInt(420);
this.anim = anim;
}
public void move() {
if (x<0 || x>940) {
velX = -velX;
stopper(x, 0);
}
if (y<0||y>420) {
velY = -velY;
stopper(0, y);
}
x += velX;
y += velY;
}
public void stopper(int x, int y) {
if (x<0 || x>940) {
t.stop();
qna.hor();
}
if (y<0||y>420) {
t.stop();
qna.ver();
}
}
public void paint (Graphics g) {
g.fillOval(x, y, 40, 40);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public void stopper(boolean b) {
// TODO Auto-generated method stub
}
}
想要一直阻止框架弹出。我还希望从弹出的JFrame中显示问题。