简体   繁体   English

在浏览器中运行时,Java Applet图像不会显示

[英]Java Applet Images will not display when run in browser

I finally managed to get my java applet to run in my browser, however I now face the issue that none of my images will display. 我终于设法使我的Java applet在浏览器中运行,但是现在我面临一个问题,那就是我的图像都不会显示。 The only thing that displays is black text that is drawn in the screen in the applet. 唯一显示的是在applet屏幕中绘制的黑色文本。

In order to get the applet to work at all, I had to export as a jar and sign it myself. 为了使该applet完全起作用,我不得不将其作为罐子导出并自己签名。 Now, I'm wondering why it is that the images won't display. 现在,我想知道为什么图像无法显示。 I checked and the jar file does include all the image files. 我检查了一下,jar文件确实包含了所有图像文件。 Likewise, the applet runs perfectly well in Eclipse. 同样,小程序在Eclipse中运行得很好。

What could be the issue here? 这里可能是什么问题?

mainscreen = new ImageIcon("main.png").getImage(); is the first image that should be accessed. 是应该访问的第一个图像。

Additionally, all of my classes including the applet class are in packages. 另外,我所有的类(包括applet类)都在包中。 The images are all outside of those packages. 图像都在那些软件包之外。

ImageIcon("main.png").getImage() suggests that you are trying to load an icon from a File , which is not the case for applets ImageIcon("main.png").getImage()建议您尝试从File加载图标,小应用程序则不是这种情况

With embedded resources you need to use getClass().getResource("/path/to/resource") . 对于嵌入式资源,您需要使用getClass().getResource("/path/to/resource") This will allow your application to search within it's class path to find the resource... 这将使您的应用程序可以在其类路径中搜索以找到资源...

Try something more like ImageIcon(getClass().getResource("/main.png")).getImage() , assuming that the main.png resides within the root folder of the jar file... 尝试更像ImageIcon(getClass().getResource("/main.png")).getImage() ,假设main.png位于jar文件的根文件夹中...

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

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