62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.*;
public class MPlus
{
public static void main(String[] args)
{
MyFrame f1=new MyFrame();
f1.launch();
}
}
class MyFrame extends Frame
{
super("Plus");
TextField t1;
TextField t2;
TextField t3;
public void launch(){
setLayout(new FlowLayout());
t1 = new TextField(10);
Label l1 = new Label("+");
t2 = new TextField(10);
Button b1 = new Button("=");
t3 = new TextField(15);
b1.addActionListener(new Monitor(this));
add(t1);
add(l1);
add(t2);
add(b1);
add(t3);
pack();
setVisible(true);
}
}
class Monitor implements ActionListener
{
MyFrame f1 = null;
public Monitor(MyFrame f2){
f1 = f2;
}
public void actionPerformed(ActionEvent e){
int n1 = Integer.parseInt(f1.t1.getText());
int n2 = Integer.parseInt(f1.t2.getText());
f1.t3.setText(""+(n1+n2));
}
}