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

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

问题描述

我终于设法让我的 Java 小程序在我的浏览器中运行,但是我现在面临的问题是我的任何图像都不会显示.唯一显示的是小程序中在屏幕上绘制的黑色文本.

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 并自己签名.现在,我想知道为什么图像不会显示.我检查过,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.

这里可能有什么问题?

mainscreen = new ImageIcon("main.png").getImage(); 是应该访问的第一个图像.

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. 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("/path/to/resource").这将允许您的应用程序在其类路径中搜索以查找资源...

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天全站免登陆