package 实训三;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculate {
JFrame f = new JFrame("算术测试");
Panel p = new Panel();
Button button1 = new Button("获取题目");
Button button2 = new Button("确认答案");
static Label label1 = new Label("你还没有回答呢");
TextField text1 = new TextField(20);
TextField text2 = new TextField(20);
TextField text3 = new TextField(20);
public void show() {
p.add(button1);
button1.addActionListener(new oListener());
p.add(text1);
p.add(new Label("+"));
p.add(text2);
p.add(new Label("="));
p.add(text3);
p.add(button2);
button2.addActionListener(new bListener());
p.add(label1);
f.add(p);
f.setBounds(20, 30, 699, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class oListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Random rand = new Random();
String s = String.valueOf(rand.nextInt(100));
String s1 = String.valueOf(rand.nextInt(100));
text1.setText(s);
text2.setText(s1);
text1.setEditable(false);
text2.setEditable(false);
}
}
public class bListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int a = Integer.valueOf(text1.getText());
int b = Integer.valueOf(text2.getText());
int c = 0;
if (text3.getText().length() >= 1)
c = Integer.valueOf(text3.getText());
if (text3.getText().length() < 1) {
label1.setVisible(true);
label1.setBounds(399, 20, 90, 50);
}
else if (a + b == c) {
System.out.println("Right!");
} else {
System.out.println("Wrong!");
}
}
}
public static void main(String[] args) {
new Calculate().show();
label1.setVisible(false);
}
}