在 Java Applet 中加载图像 [英] Loading Image in Java Applet

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

问题描述

当我尝试在小程序查看器中运行小程序时,它无法找到资源(图片).我尝试像这样加载资源:

When I try to run an applet in applet viewer it is not able to find resources (Image). I try to load resource like this:

String cb= this.getCodeBase().toString();
String imgPath = cb+"com/blah/Images/a.png";
System.out.println("imgPath:"+imgPath);
java.net.URL imgURL = Applet.class.getResource(path);

但是当我在 appet 查看器路径中运行它时是这样的:imgPath:file:D:/Work/app/build/classes/com/blah/Images/a.png

but when i run it in appet viewer path is like this: imgPath:file:D:/Work/app/build/classes/com/blah/Images/a.png

虽然在这条路径中有图像,是前缀文件:导致问题,我如何测试此代码?

though image is there in this path, is prefix file: causing problem, how can i test this code?

在服务器中部署此代码并且代码库返回服务器 URL 时,此代码是否有效?

Will this code work when deployed in server and codebase returns a server URL?

推荐答案

您的小程序是否应该在加载后加载图像?或者,您是否会更好地将必要的图像资源与您的小程序捆绑在 jar 中?

Is your applet supposed to load images after it is loaded? Or would you be better served bundling necessary image resources in the jar with your applet?

我每天都在基于小程序的应用程序上工作,该应用程序在 GUI 中具有大量图形.它们捆绑在 jar 文件中.这就是我们所做的:

I work daily on an applet-based application with plenty of graphics in the GUI. They are bundled in the jar-file. This si what we do:

// get the class of an object instance - any object.  
// We just defined an empty one, and did everything as static.
class EmptyClass{}
Class loadClass = new EmptyClass().getClass();
// load the image and put it directly into an ImageIcon if it suits you
ImageIcon ii = new ImageIcon(loadClass.getResource("/com/blah/Images/a.png"));
// and add the ImageIcon to your JComponent or JPanel in a JLabel
aComponent.add(new JLabel(ii)); 

确保您的图像实际上位于您认为它所在的罐子中.使用:

Make sure your image is actuallly in the jar where you think it is. Use:

jar -tf

...获取列表.

这篇关于在 Java Applet 中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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