我有一个从Netbeans IDE完美运行的应用程序,但是当从dist目录中的jar文件运行时,不会加载必要的图像.
我花了1个半天阅读这个和其他论坛,试图找到答案,但我无法让jar图像工作.
在这里,他是我的代码摘录:
String str = t.getText() + "100.gif";
Image img = null;
if (t != HIDDEN)
{
ClassLoader cldr = Terrain.class.getClassLoader();
URL url = cldr.getResource("fantasyhexwar/Images/" + str);
if (url == null)
JOptionPane.showMessageDialog(null, "no good");
img = ImageIO.read(url);
t.setImage(img);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多相对路径的组合,包括"images /","/ images /"等.图像在jar文件中:
fantasyhexwar/Images/plains100.gif
fantasyhexwar/Images/quarry60.gif
fantasyhexwar/Images/ram80.gif
fantasyhexwar/Images/save map40.gif
fantasyhexwar/Images/scout80.gif
fantasyhexwar/Images/settler80.gif
fantasyhexwar/Images/ship80.gif
Run Code Online (Sandbox Code Playgroud)
等等...
我知道我遗漏了一些基本的东西,但我不确定是什么.我怀疑它与清单文件或类路径有关.
希望有人可以指出我正确的方向......
编辑:问题似乎是这样
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
Run Code Online (Sandbox Code Playgroud)
返回null.图像肯定在JAR中,在绝望中我也尝试了所有可能的相对路径,代码如下:
ClassLoader cldr = Terrain.class.getClassLoader();
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
if (url == null)
url = …Run Code Online (Sandbox Code Playgroud) java ×1