为什么这个“新的ImageIcon(getClass()。getResource(”files / icon.png“))。getImage();”让我的程序看不见?

时间:2021-11-11 11:19:09

I wrote this

我写了这个

 Image icon = new ImageIcon(getClass().getResource("files/icon.png")).getImage();

in a JFrame-extending class to then set the icon of the frame with

在JFrame扩展类中,然后设置框架的图标

this.setIconImage(icon);

I do it this way, intead of by simply using the Path, because otherwise the Icon would disappear, if I exported my Project as an executable .jar. (as seen here: Once exported, java cannot find/draw images)

我这样做,只需使用Path,因为否则Icon会消失,如果我将Project导出为可执行文件.jar。 (如下所示:一旦导出,java无法查找/绘制图像)

The problem that occurs now is, that the executable .jar works the way it should (It creates a folder, as well as a .xml-file and writes into it), but without generating a visible frame. If I run the program in eclipse however, everything works fine and I see the frame. I found out, that the problem has to be in the creation of an ImageIcon, because if I run

现在出现的问题是,可执行文件.jar以它应该的方式工作(它创建一个文件夹,以及.xml文件并写入其中),但不生成可见帧。如果我在日食中运行该程序,一切正常,我看到框架。我发现问题必须在于创建一个ImageIcon,因为如果我运行的话

getClass().getResource("files/icon.png");

everything works fine. The file is at it's place, too, so this shouldn't be the mistake.

一切正常。文件也在它的位置,所以这不应该是错误。

Hope you can help me and thanks in advance.

希望你能帮助我,并提前感谢。

1 个解决方案

#1


Okay guys, it was all my fault... @JBNizet you had the right clue again... it was the case of the path "files/icon.png" -> "Files/icon.png"

好吧,伙计们,这完全是我的错... @JBNizet你再次得到了正确的线索......就是路径“files / icon.png” - >“Files / icon.png”的情况

#1


Okay guys, it was all my fault... @JBNizet you had the right clue again... it was the case of the path "files/icon.png" -> "Files/icon.png"

好吧,伙计们,这完全是我的错... @JBNizet你再次得到了正确的线索......就是路径“files / icon.png” - >“Files / icon.png”的情况