简体   繁体   English

Java applet图标不显示

[英]Java applet icon doesn't show

I have a java applet where I've changed the image icon that appears in the top left corner of the window. 我有一个Java小程序,在这里我更改了显示在窗口左上角的图像图标。 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. 一切正常,直到我将applet部署到一个独立的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/ 但是图像在里面,尽管它在以下文件夹中:myapp / resources /

What am I doing wrong here? 我在这里做错了什么? Is this some weird java bug? 这是一些怪异的Java错误吗?

Are you sure you export your source code within the jar file? 您确定要在jar文件中导出源代码吗? Because since your image is in "src/myapp/resources/logo.png", you must include your "src/myapp/resources" folder within your jar file. 因为您的图片位于“ src / myapp / resources / logo.png”中,所以您必须在jar文件中包含“ src / myapp / resources”文件夹。

But I'd recommend you to put your images in another folder, like "resources", at the root of your application folder (ie at the root of your jar file), and then you would be able to export an applet without the source code. 但是我建议您将图像放在应用程序文件夹的根目录(即jar文件的根目录)中的另一个文件夹(例如“ resources”)中,然后可以在没有源文件的情况下导出applet码。

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. 之后,我将图像复制到.class文件在jar内的相同位置,并且可以正常加载。 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. src / resources文件夹存在且内部有映像,但未加载。 I think this a path specification problem but I haven't found the solution for this yet... 我认为这是路径规范问题,但我还没有找到解决方案...

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

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