这是控制台的所显示的结果: 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(); } }
蜂之谷
相关分类