java--math加法

来源:互联网 发布:warframe网络端口 编辑:程序博客网 时间:2024/05/04 16:31
import java.awt.*;
import java.awt.event.*;


public class TFMath2
{
public static void main(String[] args)
{
new TFrame().launchFrame();
}
}
class TFrame  extends Frame
{
TextField num1,num2,num3;//设定为成员变量
public void launchFrame()
{
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(15);
Label lb = new Label("+");
Button b = new Button("=");
b.addActionListener(new listener());
setLayout(new FlowLayout());
add(num1);
add(lb);
add(num2);
add(b);
add(num3);
pack();
setVisible(true); 
}
}
class listener implements ActionListener
{
TFrame tf = null;//这样来调用不同类的成员变量,这样调用很方便
public void actionPerformed(ActionEvent e)
{
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}
}