本文介绍了在JButton的ImageIcons未显示在运行的JAR文件了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的GUI 6 Jbutton中都有它的图像,
当我编译和运行code,对Jbutton将所有图像显示完美
但在运行的JAR文件,在Jbutton将图像不显示出来..我该如何解决这个问题?

I have 6 JButtons on my GUI all have images on it,when I compile and run the code, all images on JButtons show up perfectlybut in runnable JAR file, images on JButtons are not showing up.. how do I fix this problem?

我用我的code这个方法显示在图标Jbutton将

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");
jb1 = new JButton(SettingsIc);
jb1.setFocusPainted( false );
//jb1.setBorderPainted(false); 
jb1.setContentAreaFilled(false);

这是当我编译我的code在Eclipse我的GUI的外观

This is how my GUI looks when I compile my code in Eclipse

这是我的GUI看起来如何执行的Runnable JAR文件后,

This is how my GUI looks after executing Runnable JAR file

推荐答案

这(正如许多人指出的)

This (as pointed out by a number of people)

ImageIcon SettingsIc = new ImageIcon("bin/images/settings.png");

建议您尝试从斌/图像关闭文件系统加载图像。这是你的应用程序的执行点的相对路径。

Suggests that you are trying to load the images from the bin/images off the file systems. This is a relative path from the execution point of your application.

的ImageIcon 不会抱怨。

如果可能的话,你最好嵌入你的JAR文件中的资源(这将使它更易于部署),并使用类似的getClass()。的getResource(/斌/图像/设置。 PNG)加载图像。

If possible, you are better off embedding the resources within your Jar file (it will make it easier to deploy) and use something like getClass().getResource("/bin/images/settings.png") to load the images.

如果可能的话,你应该尝试使用 ImageIO.read(URL)来加载图片,它会抛出一个异常,如果该资源指向的文件 / 网​​址不存在(或无效)。

If possible, you should try using ImageIO.read(URL) to load your images, it will throw an exception if the resource pointed to by the File/URL does not exist (or is invalid).

这篇关于在JButton的ImageIcons未显示在运行的JAR文件了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:37