java--math加法

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));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值