import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TPlus { public static void main(String[] args) { new TFFrame(); } } class TFFrame extends Frame{ TextField num1,num2,num3; public TFFrame(){ num1 =new TextField(10); num2 =new TextField(10); num3 =new TextField(15); Label lblplus =new Label("+"); Button equ =new Button("="); equ.addActionListener(new MyMonitor(this)); setLayout (new FlowLayout()); add(num1); add(lblplus); add(num2); add(equ); add(num3); pack(); setVisible(true); } } class MyMonitor implements ActionListener{ TFFrame tf=null;//!!!!好方法获得另一个类的东西之和那个对象打交道 public MyMonitor(TFFrame tf){ this.tf=tf; } public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(tf.num1.getText()); int n2=Integer.parseInt(tf.num2.getText()); tf.num3.setText(String.valueOf(n1+n2)); } }
我只想实现一个很简单的加法计算器,以上程序能够正确实现,但为啥我把哪一行改
成
TFFrame tf=new TFFrame (); //就报错Exception in thread "main" java.lang.StackOverflowError
相关分类