Java Swing:从jar文件运行时图像不显示 [英] Java Swing: Images not Displaying when run from jar file
问题描述
Swing应用程序及其图像在Eclipse中运行良好.但是当我从 jar 文件运行时,图像无法显示.我尝试以不同的方式加载图像,但是一段时间的jar文件未执行.
Swing application and it's Images are running fine in eclipse. but when i run from jar file, images not displaying. I tryed to load images in different way, but some time jar file not executing.
我的图像文件夹在源文件夹的内部.
My image folder is inside of source folder.
我的图像加载有:
1.在eclipse中运行良好,但在jar中却不能运行
1.Running fine in eclipse, but not in jar
ImageIcon right = new ImageIcon("./chack img/Green Tick 1.PNG");
2.在eclipse中运行良好,但jar文件未执行.
2.Running fine in eclipse, but jar file not executing.
ImageIcon wrong = new javax.swing.ImageIcon(getClass().getResource("/chack img/Red Cross1.PNG"));
我也尝试了其他一些代码,但问题仍然没有解决.
I tryed some other code also, still my issue not solved.
推荐答案
将图像和类一起放入src文件夹中.可选,但要制作一个用于存储图像的软件包.编译jar文件不会保留任何不是源目录的目录.
此外,从文件夹和文件名中删除所有空格.这样可以避免混乱,并且很可能是问题的一部分.在编程以及大多数命名文件夹和文件的情况下,不遵循空格是一种好的做法.
Also, remove all spaces from your folders and file names. This prevents confusion, and is likely part of your problem. No spacing is a good practice to follow in programming, and in most situations of naming folders and files.
此外, kiheru 是正确的,出于相同的原因,您不应在文件扩展名中使用大写字母没有空间;它可能会引起混乱,并且在大多数情况下是个坏习惯.
Also, kiheru is correct, you should not have capital letters in the file extensions, for the same reason you should not have spaces; It can cause confusion and it is a bad practice in most cases.
这篇关于Java Swing:从jar文件运行时图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!