Java 小程序图标不显示

发布于 2024-08-29 08:05:47 字数 379 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-09-05 08:05:47

您确定在 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.

苍风燃霜 2024-09-05 08:05:47

我已经设法找到解决方法。我将:更改

Image frameIcon = kit.getImage("src/myapp/resources/logo.png");     

Image frameIcon = kit.getImage("logo.png");     

然后部署 jar。之后,我将图像复制到 jar 内 .class 文件所在的位置,并且加载正常。
我不喜欢这种解决方法,但现在必须这样做。 src/resources 文件夹存在并且内部有图像,但无法加载。我认为这是路径规范问题,但我还没有找到解决方案......

I have managed to find a workaround for this. I changed the:

Image frameIcon = kit.getImage("src/myapp/resources/logo.png");     

to

Image frameIcon = kit.getImage("logo.png");     

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文