导出的 JAR 无法读取图像 [英] Exported JAR Won't Read Image
本文介绍了导出的 JAR 无法读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让图像显示在 JPanel 中.这是我正在使用的代码:
I am attempting to get an image to show up in a JPanel. Here is the code I'm using:
URL imageURL;
BufferedImage image = null;
ImageIcon icon;
imageURL = getClass().getClassLoader().getResource("images/audiorpglogo.png");
if (imageURL == null) {
System.out.println(imageURL == null);
try { imageURL = new File("images/audiorpglogo.png").toURI().toURL(); }
catch (Exception e1) { imageURL = null; }
}
System.out.println(imageURL == null);
try { image = ImageIO.read(imageURL); }
catch (Exception e) { }
System.out.println(image == null);
icon = new ImageIcon(image);
System.out.println(icon == null);
logo = new JLabel(icon);
当我在 Eclipse 中运行这个程序时,我得到以下输出:
When I run this program in Eclipse, I get the following output:
true
false
false
false
但是,当我将程序导出为可运行的 JAR 时,我得到以下输出:
However, when I export the program as a runnable JAR, I get this following output:
true
false
true
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
at me.pogostick29.audiorpg.window.Window.<init>(Window.java:85)
at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:30)
预先感谢您的帮助!
推荐答案
您应该有一个资源文件夹,其中包含名为 images
的文件夹,然后它应该可以工作.
You should have a resource folder with the folder named images
in that, then it should work.
示例:
我如何访问这些图标:
public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");
private BufferedImage loadBufferedImage(String string)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
return bi;
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
这篇关于导出的 JAR 无法读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文