- package Ketangcg;
- import java.awt.*;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class DemoUI {
- private Frame f;
- private Button bt;
- private Label l;
- DemoUI(){
- init();
- }
- public static void main(String[] args)
- {
- DemoUI d=new DemoUI();
- System.out.println("sa");
- }
- private void myEvent()
- {
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e)
- {
- System.out.println("关闭");
- System.exit(0);
- }
- });
- }
- private void init(){
- Frame f=new Frame("my swing");
- Button bt=new Button("按钮");
- Label l=new Label();
- f.add(l);
- f.add(bt);
- f.setSize(500,300);
- f.setLocation(300,200);
- f.setLayout(new FlowLayout());
- f.setVisible(true);
- myEvent();
- }
- }
复制代码 我的eclipse运行报异常。
Exception in thread "main" java.lang.NullPointerException
at Ketangcg.DemoUI.myEvent(DemoUI.java:26)
at Ketangcg.DemoUI.init(DemoUI.java:45)
at Ketangcg.DemoUI.<init>(DemoUI.java:13)
at Ketangcg.DemoUI.main(DemoUI.java:19)
|