gpt4 book ai didi

javax.imageio.ImageIO - 无法将图标加载到 JFrame

转载 作者:行者123 更新时间:2023-11-29 03:52:26 31 4
gpt4 key购买 nike

在下面的代码片段中,我尝试使用 javax.imageio.ImageIO 设置我的 JFrame 实例的图标,但出现以下异常。你能告诉我我缺少什么吗?

    import javax.imageio.ImageIO;
......
BufferedImage image = null;
try {
//EXCEPTION IS thrown in the following line
image = ImageIO.read(this.getClass().getResource("resources/Smartbook_icon.ico"));
} catch (IOException e) {
......
}
this.setIconImage(image);

异常堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:  input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at se.websiter.bookingapp.UI.MainFrame.setGUIIcon(MainFrame.java:4131)
at se.websiter.bookingapp.UI.MainFrame.<init>(MainFrame.java:59)
at se.websiter.bookingapp.UI.MainFrame$60.run(MainFrame.java:4167)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

可能是资源路径不正确。我总是使用以下代码片段来设置 JFrame 的图标图像,假设 resources 是类包的子目录:

import java.net.URL;

// Setting window's icon
String resourcePathToIcon = String.format("/%s/resources/myicon.png",
MyClass.class.getPackage().getName().replace('.', '/'));
URL windowIconURL = MyClass.class.getResource(resourcePathToIcon);
if (windowIconURL != null)
setIconImage(new ImageIcon(windowIconURL).getImage());

请注意,资源图像的路径以 / 开头。

关于javax.imageio.ImageIO - 无法将图标加载到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8113645/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com