简体   繁体   English

作为Jar运行时找不到图像

[英]Image not found when running as Jar

I want to ask about a problem in my project: I have a class that extends JFrame , and I want to change the Java icon on that frame to another icon using this code: 我想问一个项目中的问题:我有一个扩展JFrame的类,并且我想使用以下代码将该框架上的Java图标更改为另一个图标:

setIconImage(new ImageIcon(getClass().getResource("icon.PNG")).getImage());

I'm using NetBeans , and when I run my project from NetBeans, it runs normally, but when I run it from the jar, it does not run. 我正在使用NetBeans ,当我从NetBeans运行项目时,它可以正常运行,但是当我从jar中运行它时,它不会运行。 Then, when I remove that code, my jar runs normally with the Java icon in the frame. 然后,当我删除该代码时,我的jar会正常运行,框架中带有Java图标。

Can anyone can tell me what might be wrong with that code? 谁能告诉我该代码有什么问题吗?

When running jar file you cannot access files by using: 运行jar文件时,无法使用以下命令访问文件:

getClass().getResource("...")

You should use: 您应该使用:

getClass().getResourceAsStream("...")

See: File loading by getClass().getResource() 请参阅: 通过getClass()。getResource()加载文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM