我知道以前有人问过这个问题(或类似的问题),但我还是不明白。我创建了一个具有以下内部结构的 jar:
JAR文件
-配置
---codeBehind
---命令行
---图形用户界面
---资源
在资源文件夹中,我有一张图像想用作图标,但我不知道如何获取它。我试过了
ImageIcon icon = new ImageIcon(getClass().getResource("settings.png"));
setIconImage(icon.getImage());
但是当我试图访问图标的图像时它抛出一个NullPointerException
。我试图分开命令的各个部分,getResources("mypic.png")
返回 null。
关于如何获得它的任何想法?提前致谢!
最佳答案
来自 the documentation of Class :
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form: modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
所以你可能想要
getResource("/settings.png")
或
getResource("/resources/settings.png")
或类似的东西,取决于你的类路径,我相信。
关于java - 如何从 JAR 中访问图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2381754/