62,628
社区成员
发帖
与我相关
我的任务
分享import java.awt.*;
import java.awt.event.*;
public class TFMath {
public static void main(String args[]) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
private static final long serialVersionUID = 1L;
TextField num1, num2, num3;
public void launchFrame() {
//Frame f1 = new Frame("math");//这句话没必要..
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Label lab1 = new Label("+");
Button b1 = new Button("=");
b1.addActionListener(new MyMonitor());
setLayout(new FlowLayout());
add(num1);
add(lab1);
add(num2);
add(b1);
add(num3);
pack();
setVisible(true);
}
class MyMonitor implements ActionListener {//这里写成内部类..
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
System.out.println("" + (n1 + n2));
num3.setText("" + (n1 + n2));
}
}
}