2016-08-18 113 views
1

我试图导出包含som图像的java项目,但导出后图像不显示。我读过这些类似的问题和答案:将java项目导出为带有图像的可运行jar文件

Eclipse exported Runnable JAR not showing images

Exporting Images with JAR in Eclipse (Java)

我已经尝试了所有那些建议,但图像还是不导出项目后显示的不同选项。

我试过这个文件结构:

enter image description here

采用这种结构我使用此代码创建图像:

Image picture = new Image("file:src/Pictures/download.png"); 

而这个文件结构:

enter image description here

采用这种结构我使用此代码创建图像:

Image picture = new Image("file:Pictures/download.png"); 

当导出我选择“包所需的库到生成JAR”的项目:

enter image description here

的图像当我从Eclipse运行程序时出现,但是一旦我导出它们就会消失。我究竟做错了什么?我真的觉得我已经尝试了一切,但也许有一些我错过的简单细节?

谢谢!

回答

1

问题不在于创建jar文件,它与您阅读图像的方式不同,请在下面的代码中读取图像,它将起作用。

ImageIO.read(ClassLoader.getSystemResource("image/button1.png")); 
+0

好的,你能解释一下吗?我退出了新的javafx,所以我不是专家;)该代码似乎返回一个缓冲的图像。我想将我的图像放入图像视图中以在程序中显示它。但是,在创建图像视图时,似乎无法使用缓冲图像作为输入参数。我如何让缓冲的图像出现在程序中? – ekstroom

+0

我用这个代码,现在它工作正常: ImageView imgViewPicture = new ImageView(); \t \t尝试{ \t \t \t BufferedImage buffered = ImageIO.read(ClassLoader.getSystemResource(“Pictures/download.png”)); \t \t \t Image picture = SwingFXUtils.toFXImage(buffered,null); \t \t \t \t \t \t imgViewPicture = new ImageView(picture); \t \t \t imgViewPicture.setPreserveRatio(true); \t \t \t imgViewPicture。setFitHeight(250); \t \t}赶上(IOException的发送){ \t \t \t // TODO自动生成的catch程序块 \t \t \t e.printStackTrace(); \t \t} 谢谢你的帮忙! :) – ekstroom

+0

'InputStream stream = getClass()。getResourceAsStream(“/ resources/icons/xyz.png”); ImageIcon icon = new ImageIcon(ImageIO.read(stream))'。你可以通过'icon.getImage()'来获取图像对象 –

相关问题