寫到最後卡在我重新出題的按鈕設不好,目前寫到有辦法改變題目,但無法重新判斷答案對錯。
不知道有沒有辦法改變我程式中F3的值能讓判定更新。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test011
{public static void main(String args[])
{JFrame frame=new JFrame("Key Event");
JPanel X1=new JPanel();
Container cp=frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
cp.setLayout(null);
int A1;
int A2;
final JLabel B1=new JLabel("");
final JLabel B2=new JLabel("");
JButton B3=new JButton("下一題");
final JLabel B4=new JLabel("請輸入答案");
JLabel B5=new JLabel("+");
JLabel B6=new JLabel("=");
final JTextField B7=new JTextField();
final JLabel B8=new JLabel("");
B1.setBounds(35,60,40,30);
B2.setBounds(75,60,40,30);
B3.setBounds(55,10,80,20);
B4.setBounds(30,100,140,30);
B5.setBounds(55,60,20,30);
B6.setBounds(100,60,20,30);
B7.setBounds(110,65,30,20);
B8.setBounds(0,0,30,20);
cp.add(B1);
cp.add(B2);
cp.add(B3);
cp.add(B4);
cp.add(B5);
cp.add(B6);
cp.add(B7);
double C1;
double D1;
C1=Math.random()*100;
D1=Math.random()*100;
A1=(int)C1;
A2=(int)D1;
String E1 =Integer.toString(A1);
String E2 =Integer.toString(A2);
B1.setText(E1);
B2.setText(E2);
String F1=Integer.toString(A1+A2);
B8.setText(F1);
final String F3=B8.getText();
B7.addActionListener(new ActionListener(){
public String win;
public void actionPerformed(ActionEvent ev){
win=B7.getText();
if(win.equals(F3)){
B4.setText("您答對了!");
}
else{
B4.setText("錯了!正確答案是"+F3);
}
}
}
);
B3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int oo1;
int oo2;
double o1;
double o2;
o1=Math.random()*100;
o2=Math.random()*100;
oo1=(int)o1;
oo2=(int)o2;
String OA =Integer.toString(oo1);
String OB =Integer.toString(oo2);
B1.setText(OA);
B2.setText(OB);
String F2=Integer.toString(oo1+oo2);
B8.setText(F2);
}
});
}
}