Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1

  2. #2

    Swing tạo menu bar không bắt lỗi nhưng không hiện giao diện

    Code thì ko thấy lỗi, chạy xong báo successfully nhưng cái giao diện thì chẳng thấy đâu. Bạn nào giúp mình xem nguyên nhân tại sao, thanks!!!

    Mã:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package simplemenubar;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    
    public class SimpleMenuBar extends JFrame {
    
        /**
         * @param args the command line arguments
         */
        public SimpleMenuBar()
        {
            initUI();
        }
        public final void initUI()
        {
            JMenuBar menubar=new JMenuBar();
            ImageIcon icon=new ImageIcon(getClass().getResource("exit.png"));
            
            JMenu file=new JMenu("File");
            file.setMnemonic(KeyEvent.VK_F);
            
            JMenuItem eMenuItem=new JMenuItem("Exit",icon);
            eMenuItem.setMnemonic(KeyEvent.VK_C);
            eMenuItem.setToolTipText("Exit Application");
            eMenuItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    System.exit(0);
                }
            });
            file.add(eMenuItem);
            
            menubar.add(file);
            
            setJMenuBar(menubar);
            
            setTitle("Simple Menu");
            
            setSize(300,200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
        }
        public static void main(String[] args) {
            // TODO code application logic here
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    SimpleMenuBar sm=new SimpleMenuBar();
                    sm.setVisible(true);
                }
            });
        }
    }
    chạy xong compiler báo thế này:

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at simplemenubar.SimpleMenuBar.initUI(SimpleMenuBar.j ava:30)
    at simplemenubar.SimpleMenuBar.<init>(SimpleMenuBar.j ava:25)
    at simplemenubar.SimpleMenuBar$2.run(SimpleMenuBar.ja va:64)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 697)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    BUILD SUCCESSFUL (total time: 3 seconds)

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •