【发布时间】:2013-09-04 01:28:06
【问题描述】:
我正在尝试从可执行 JAR 文件加载图像。
这是检索图像的函数:
public static ImageIcon loadImage(String fileName, Object o) {
BufferedImage buff = null;
try {
buff = ImageIO.read(o.getClass().getResource(fileName));
// Also tried getResourceAsStream
} catch (IOException e) {
e.printStackTrace();
return null;
}
if (buff == null) {
System.out.println("Image Null");
return null;
}
return new ImageIcon(buff);
}
这就是它的名称:
logo = FileConverter.loadImage("/pictures/Logo1.png", this);
JFrame.setIconImage(logo.getImage());
this 是一个简单的对象。 我也没有得到 NullPointerException,除非它被 UI 屏蔽。
我检查了 JAR 文件,图像位于:
/pictures/Logo1.png
当前代码在 eclipse 中和导出到 JAR 并在终端中运行时都有效,但在双击 JAR 时无效,在这种情况下,图标是默认的 JFrame 图标。
感谢您的帮助。可能只有我遗漏了一些明显的东西。
【问题讨论】:
-
即使我双击
JAR文件或通过command prompt运行它,它在我这边也能正常工作。我在 eclipse juno 中创建了 this project,你也可以测试一下 :-) -
如果你直接这样做 -
return new ImageIcon(getClass().getResource("/pictures/Logo1.png"));会发生什么?
标签: java image swing jar embedded-resource