我正在学习 Java Swing 库,但遇到了问题。
在示例程序中,它通过以下行创建一个ImageIcon对象:
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
当我执行我的程序时,我收到以下错误,原因是项目中缺少exit.png:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at com.andrea.second.SimpleMenu.initUI(SimpleMenu.java:23)
at com.andrea.second.SimpleMenu.<init>(SimpleMenu.java:17)
at com.andrea.second.SimpleMenu$2.run(SimpleMenu.java:53)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
问题是,使用 Eclipse,我必须把这个图像放在哪里?
我尝试将“exit.png”文件(在我的文件系统中,而不是在 Eclipse 中)复制并粘贴到包含创建它的类的同一个包文件夹中......但它不起作用
我该怎么做才能在 Eclipse 中解决这个问题?