【发布时间】:2021-01-03 17:32:40
【问题描述】:
我正在尝试用 java 做一个小项目,但没有运气 如果我用 eclipse 编译程序一切都很好,但是当我创建 jar 文件时,我得到一个空白窗口
这是我声明的图片:
public ImageIcon BACKGROUND = new ImageIcon() ;
我尝试做以下事情:
1.
new ImageIcon("Images/wood.jpg").getImage());
2.
this.BACKGROUND.setImage(Toolkit.getDefaultToolkit().getImage("Images/wood.jpg"));
3.
this.BACKGROUND = new ImageIcon(getClass().getResource("Images/wood.jpg"));
4.
/** Returns an ImageIcon, or null if the path was invalid. */
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
1 & 2 显示编译后的图像,3 & 4 返回 null
另一个想法是我使用的是 mac,当我使用 windows 时,编译后没有显示图像。
【问题讨论】:
-
您是否将图像作为资源包含在 jar 文件本身中?如果没有,您可能应该这样做。
标签: java