为了账号安全,请及时绑定邮箱和手机立即绑定

救命!!TFFrame tf=null;和TFFrame tf=new TFFrame();有啥区别?

救命!!TFFrame tf=null;和TFFrame tf=new TFFrame();有啥区别?

qq_代号猎人_0 2016-12-06 21:31:05
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
查看完整描述

1 回答

  • 1 回答
  • 1 关注
  • 1577 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号