程序如下: package com.zyn_1; import java.awt.*; import java.awt.event.*; public class WriteNote { private Frame f; private MenuBar mb; private Menu m1,m2; private MenuItem mi1,mi2,mi3,mi4,mi5,mi6; private FileDialog open,save; private TextArea ta; WriteNote(){ init(); } public void init(){ f = new Frame("记事本");//创建frame窗体 //对窗体进行基本设置 f.setLayout(new FlowLayout());//设置布局 f.setLocation(300, 70);//位置 f.setSize(600,640); //大小 m1 = new Menu ("文件"); m2 = new Menu("帮助"); mi1 = new MenuItem("新建"); mi2 = new MenuItem("打开"); mi3 = new MenuItem("保存"); mi4 = new MenuItem("另存为"); mi5 = new MenuItem("退出"); mi6 = new MenuItem("版本"); mb.add(m1); mb.add(m2); m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4); m1.add(mi5); m2.add(mi6); open = new FileDialog(f,"打开",FileDialog.LOAD); save = new FileDialog(f,"保存",FileDialog.SAVE); ta = new TextArea(); f.add(ta); myEvent(); f.setMenuBar(mb); f.setVisible(true);//通过setvisiable(true)显示窗体 } public void myEvent(){ mi2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub open.setVisible(true); } }); mi5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); 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(); } } 控制台显示内容: Exception in thread "main" java.lang.NullPointerException at com.zyn_1.WriteNote.init(WriteNote.java:32) at com.zyn_1.WriteNote.<init>(WriteNote.java:15) at com.zyn_1.WriteNote.main(WriteNote.java:78)
redstonesl
相关分类