- package day22;
- import java.awt.*;
- import java.awt.event.*;
- public class MyMenuTest
- {
- private Frame f;
- private MenuBar bar;
- private Menu fileMenu;
- private MenuItem openItem,saveItem,closeItem;
- MyMenuTest()
- {
- init();
- }
- public void init()
- {
- f = new Frame("my window");
- f.setBounds(300,100,500,600);
- f.setLayout(new FlowLayout());
- bar = new MenuBar();
- openItem = new MenuItem("打开");
- saveItem = new MenuItem("保存");
- closeItem = new MenuItem("退出");
- fileMenu.add(openItem);
- fileMenu.add(saveItem);
- fileMenu.add(closeItem);
- bar.add(fileMenu);
- f.setMenuBar(bar);
- myEvent();
- f.setVisible(true);
-
- }
- private void myEvent()
- {
- closeItem.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- public static void main(String[] args)
- {
- new MyMenuTest();
- }
- }
复制代码 Exception in thread "main" java.lang.NullPointerException
at day22.MyMenuTest.init(MyMenuTest.java:23)
at day22.MyMenuTest.<init>(MyMenuTest.java:12)
at day22.MyMenuTest.main(MyMenuTest.java:52)
视频中的一道题,自己敲代码时有NullPointerException异常,求大牛解答 |