我一直在尝试让图像资源显示在我正在开发的 GUI 上,但很难让它们显示出来。我遇到过有关加载资源的其他问题,例如java-swing-displaying-images-from-within-a-jar和images-will-not-work-in-a-jar-file,但它们不起作用为了我。
我的问题似乎与第一个链接相同,从 Eclipse 运行时会出现图像,而使用 Jars 从命令行运行时不会出现图像。但是,这些问题的解决方案不会使图像出现。
我用于检索资源的代码是:
public class R {
public static final String resourcePackage = "path/to/image/package";
/**
* Returns the resource string that refers to the <code>resource</code> file
* in the <code>path.to.image.package.png</code> package.
*
* @param resource
* the file in the <code>png</code> package
* @return the full resource string
*/
public static String png(String resource) {
return String.format("%s/png/%s", resourcePackage, resource);
}
public static ResizableIcon resizableIcon(String resource) {
return ImageWrapperResizableIcon.getIcon(R.class.getClassLoader()
.getResourceAsStream(resource), new Dimension(48, 48));
}
}
我在生成 GUI 时调用它
JCommandButton connect = new JCommandButton(i18ln.getString("ports"),
R.resizableIcon(R.png("serial-port-32x32.png")));
打印语句指示已找到资源,因为R.class.getClassLoader().getResourceAsStream
返回sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream
.
我难住了。我花了几个小时试图弄清楚这一点,但可以使用一些帮助。有任何想法吗?
仅供参考:我认为这并不重要,但我使用Flamingo作为我的 GUI。
编辑:根据 Stefan 的要求
源/ 主要的/ 爪哇/ com.company.project(包) R.java MyGui.java 资源/ com.company.project(包) .png(包) 串行端口 32x32.png (更多图片) .i18ln(包) MyGui.properties
至于更多代码,我不知道我还能提供什么对这个问题有很大好处。上面提供了用于检索资源的所有代码以及我如何使用该代码。您是否正在寻找特定的东西?
更新:
当我使用 Eclipse 创建一个 Jar 并从命令行运行它时,图像资源会正确显示。当我使用 Gradle 创建 Jar 时,不显示图像。因此,在生成允许通过 Eclipse Jar 而不是 Gradle Jar 正确访问图像资源的 Jar 时,有一些不同的做法。关于这个问题,我在 Gradle 论坛上提出了一个问题。