图像在使用JAR文件时不会显示出来。

huangapple go评论62阅读模式
英文:

Image won't show up when using JAR file

问题

以下是您要翻译的内容:

所以我正在制作一个基本的国际象棋游戏,其中使用图像来表示棋子。

当我在Eclipse中运行程序时,一切都正常,如预期的那样。但是,当我使用Eclipse导出然后运行该程序时,它会出现错误 java.imageio.IIOException: 无法读取输入文件!

该图像存储在源文件夹中,位于名为images的包中。

我使用以下代码加载图像:

BufferedImage image = null;

try {
    image = ImageIO.read(new File("src/images/Chess_Pieces.png"));
} catch (IOException e) {
    System.out.println(e);
}

我尝试将图像定位到许多不同的位置,并尝试了不同的加载图像的方法,但它们都不起作用,并且我确保图像在导出的JAR文件中实际上正确显示。

英文:

So I'm using an image for the chess pieces in this basic chess game I'm making.

When I run the program in Eclipse, it works perfectly fine as expected. But when I use Eclipse to export and then run that program, it gives the error java.imageio.IIOException: Can't read input file!

The image is stored in the source folder in a package names images.

I load the image using

BufferedImage image = null;

try {
    image = ImageIO.read(new File("src/images/Chess_Pieces.png"));
} catch (IOException e) {
    System.out.println(e);
}

I've tried locating the image to many different places and I've tried different ways of loading the image, none of them work and I have made sure that the image actually appears correctly in the exported JAR file.

答案1

得分: 0

更改为:

image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));

参考:

英文:

Change it:

image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));

See :

huangapple
  • 本文由 发表于 2020年7月31日 14:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63186743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定