我一直在尋找互聯網試圖找出如何添加一個圖標圖像到我的JFrame,但我不斷收到錯誤。我知道這已被問到堆棧溢出,但解決方案不適合我。這裏是我的代碼:JFrame圖標圖像不工作
ImageIcon imageIcon = new ImageIcon("src/slime.png");
ImageIcon image = new ImageIcon("src/slime.gif");
JLabel label = new JLabel(image, JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
label.setIcon(image);
JFrame window = new JFrame("Slime");
window.setVisible(true);
window.setSize(250, 200);
window.setResizable(false);
window.setIconImage(newImageIcon(getClass().getResource("src/slime.png")).getImage());
window.add(label);
以下是錯誤我得到: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:205) at MainJFrame.<init>(MainJFrame.java:39) at MainJFrame$1.run(MainJFrame.java:18) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java: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.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:697) 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)
幫助將是非常讚賞。注意:我試過window.setIconImage(imageIcon.getImage());但這不起作用,並使我在屏幕上打印的其他圖像消失。
發生了什麼錯誤? – Vulcan
你能在這裏粘貼你的錯誤嗎? – kukis
那麼當我嘗試window.setIconImage(ImageIO.read(「src/slime.png」));我得到錯誤類型ImageIO中的方法read(File)不適用於參數(字符串) – Cj1m