这是控制台的所显示的结果:
Exception in thread "main" java.lang.NullPointerException
at com.zyn_1.WriteNote.myEvent(WriteNote.java:36)
at com.zyn_1.WriteNote.init(WriteNote.java:30)
at com.zyn_1.WriteNote.<init>(WriteNote.java:14)
at com.zyn_1.WriteNote.main(WriteNote.java:46)
这是源代码:package com.zyn_1;
import java.awt.*;
import java.awt.event.*;
public class WriteNote {
private Frame f;
private MenuBar mb;
private Menu m;
private MenuItem mi;
WriteNote(){
init();
}
public void init(){
Frame f = new Frame("记事本");//创建frame窗体
//对窗体进行基本设置
f.setLayout(new FlowLayout());//设置布局
f.setLocation(300, 100);//位置
f.setSize(500,600); //大小
mb = new MenuBar();
m = new Menu("文件");
mi = new MenuItem("退出");
mb.add(m);
m.add(mi);
myEvent();
f.setMenuBar(mb);
f.setVisible(true);//通过setvisiable(true)显示窗体
}
public void myEvent(){
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new WriteNote();
}
}
1 回答
已采纳
蜂之谷
TA贡献564条经验 获得超863个赞
空指针啊,Frame f = new Frame("记事本");//创建frame窗体,这行把Frame去掉,改成 f = new Frame("记事本"); 这么说应该懂了吧
添加回答
举报
0/150
提交
取消