在JApplet中添加图像 [英] Adding image in JApplet

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

问题描述

ImageIcon icon= new ImageIcon("a.gif");
JLabel jLabel1=new JLabel(icon);
jLabel1.setVisible(true);
card1.add(jLabel1);

我是Java的新手,我在applet的面板中添加图像时遇到了问题。我的图片位于同一个文件夹中。我的小程序可见而没有任何问题,但只显示图像。

I am a newbie to Java and I am facing a problem to add image in a panel in applet. My image is in the same folder. My applet is visible without any problem but only image is not displayed.

推荐答案

public void init() 
    URL imageURL = new URL(getDocumentBase(), "a.gif");
    Image image = getImage(imageURL);
    ImageIcon icon = new ImageIcon(image);
    // ...

ImageIcon 接受 String 的构造函数假设字符串代表路径&文件名文件

The ImageIcon constructor that accepts a String presumes the string represents the path & file name of a File.

只有受信任的applet才能访问文件,然后仅在客户端文件系统(不是服务器)上。如果这是一个应用程序资源,它应该在服务器上,并且可以通过URL访问。

Only trusted applets can access a File, and then only on the client file-system (not the server). If this is an application resource, it should be on the server, and can be accessed by URL.

请注意 ImageIcon 构造函数也将接受一个URL,而不是上面使用的 Image 。我只想强调applet有一个内置的方法来获取图像。

Note that the ImageIcon constructor will also accept an URL, rather than the Image used above. I just wanted to highlight that applets have an inbuilt method to obtain images.

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

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