Java 小程序图标不显示
我有一个 java 小程序,我在其中更改了窗口左上角显示的图像图标。我使用的代码是这样的:
Toolkit kit = Toolkit.getDefaultToolkit();
Image frameIcon = kit.getImage("src/myapp/resources/logo.png");
getFrame().setIconImage(frameIcon);
一切正常,直到我将小程序部署到独立的 jar 中。在这种情况下,显示的图标是默认图标,就好像代码找不到图像一样。但是图像在里面,尽管它在文件夹中: myapp/resources/
我在这里做错了什么? 这是一些奇怪的 java bug 吗?
I have a java applet where I've changed the image icon that appears in the top left corner of the window. The code I use is this:
Toolkit kit = Toolkit.getDefaultToolkit();
Image frameIcon = kit.getImage("src/myapp/resources/logo.png");
getFrame().setIconImage(frameIcon);
Everything works fine until I deploy the applet to a standalone jar. In this case the icon that shows is the default icon, as if the code couldn't find the image. But the image is inside, although it is in the folder: myapp/resources/
What am I doing wrong here?
Is this some weird java bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定在 jar 文件中导出源代码吗?因为由于您的图像位于“src/myapp/resources/logo.png”中,所以您必须在 jar 文件中包含“src/myapp/resources”文件夹。
但我建议您将图像放在另一个文件夹中,例如“资源”,位于应用程序文件夹的根目录(即 jar 文件的根目录),然后您就可以在没有源的情况下导出小程序代码。
Are you sure you export your source code within the jar file? Because since your image is in "src/myapp/resources/logo.png", you must include your "src/myapp/resources" folder within your jar file.
But I'd recommend you to put your images in another folder, like "resources", at the root of your application folder (i.e. at the root of your jar file), and then you would be able to export an applet without the source code.
我已经设法找到解决方法。我将:更改
为
然后部署 jar。之后,我将图像复制到 jar 内 .class 文件所在的位置,并且加载正常。
我不喜欢这种解决方法,但现在必须这样做。 src/resources 文件夹存在并且内部有图像,但无法加载。我认为这是路径规范问题,但我还没有找到解决方案......
I have managed to find a workaround for this. I changed the:
to
and then deploy the jar. After, I copy the image to the same place where the .class file is inside the jar and it loads ok.
I don't like this workaround but it will have to do for now. The src/resources folder exists and has the image inside but it doesn't load. I think this a path specification problem but I haven't found the solution for this yet...