新手java GUI

package practice;

import java.awt.*;

import java.awt.event.*;
public class TFMath {
    public static void main(String args []){
        new TTFrame().launchFrame();

    }

}

class TTFrame extends Frame{
    TextField num1,num2,num3;
    public void launchFrame(){
        TextField num1=new TextField(10);
        TextField num2=new TextField(10);
        TextField num3=new TextField(15);
        setLayout(new FlowLayout());
        Button b1=new Button("=");
        Label l=new Label("+");
        b1.addActionListener(new MyMonitor());
        add(num1);
        add(l);
        add(num2);
        add(b1);
        add(num3);
        pack();
        setVisible(true);
    }
    class MyMonitor implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            int n1=Integer.parseInt(num1.getText());
            int n2=Integer.parseInt(num2.getText());
            num3.setText("" + (n1+n2));

    }
    }
}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at practice.TTFrame$MyMonitor.actionPerformed(TFMath.java:34)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
在myeclipse上运行 输入1,2 点击“=”出错误。求指点,谢谢。

阅读 2.8k
评论
    1 个回答
    TextField num1=new TextField(10);
    TextField num2=new TextField(10);
    TextField num3=new TextField(15);
    

    这里声明错误,应该是

    num1=new TextField(10);
    num2=new TextField(10);
    num3=new TextField(15);
    
      撰写回答

      登录后参与交流、获取后续更新提醒

      相似问题
      推荐文章