在浏览器中运行时的Java Applet不显示图像 [英] Java Applet Images will not display when run in browser

查看:644
本文介绍了在浏览器中运行时的Java Applet不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于让我的Java小程序在浏览器中运行,但我现在面临着没有我的图像显示的问题。显示的唯一的事情是在applet的屏幕上绘制黑色文本。

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. The only thing that displays is black text that is drawn in the screen in the applet.

为了让小程序在所有的工作,我不得不导出为一个罐子,并签署它自己。现在,我不知道为什么它是图像将不显示。我查了一下jar文件不包括所有的图像文件。同样,小程序完全运行良好,在Eclipse。

In order to get the applet to work at all, I had to export as a jar and sign it myself. 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. Likewise, the applet runs perfectly well in Eclipse.

还有什么比这里是问题?

What could be the issue here?

mainscreen =新的ImageIcon(main.png)的getImage(); 是要访问的第一个图像

mainscreen = new ImageIcon("main.png").getImage(); is the first image that should be accessed.

此外,我所有的类,包括applet类都在包。这些图像是那些软件包的所有外。

Additionally, all of my classes including the applet class are in packages. The images are all outside of those packages.

推荐答案

的ImageIcon(main.png)。的getImage()建议您尝试加载从图标文件,这是不是小应用程序的情况下

ImageIcon("main.png").getImage() suggests that you are trying to load an icon from a File, which is not the case for applets

你需要使用嵌入式资源的getClass()。的getResource(/路径/到/资源)。这将使您的应用程序它的类路径中进行搜索查找资源...

With embedded resources you need to use getClass().getResource("/path/to/resource"). This will allow your application to search within it's class path to find the resource...

尝试更多的东西,比如的ImageIcon(的getClass()。的getResource(/ main.png))。的getImage(),假设 main.png 所在的jar文件的根文件夹内...

Try something more like ImageIcon(getClass().getResource("/main.png")).getImage(), assuming that the main.png resides within the root folder of the jar file...

这篇关于在浏览器中运行时的Java Applet不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆