英文:
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"));
参考:
- https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream/676273#676273
- https://stackoverflow.com/questions/9864267/loading-image-resource
英文:
Change it:
image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));
See :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论