Icon
接口类,有以下方法
int getIconHeight();
int getIconWidth();
void paintIcon(Component c, Graphics g, int x, int y);
ImageIcon
构造方法
ImageIcon(byte[] imageData)
ImageIcon(Image image)
ImageIcon(String filename)
ImageIcon(URL location)
实现Icon的从图像绘制图标。
使用MediaTracker预先从URL,文件名或字节数组中获取创建的图像,实现图像的加载。
Image
是所有图像类的超类,必须以平台特定的方式获取图像。
获得图像
imageicon.getImage();
各个Image的区别
ImageIcon可以通过Image转化过来
Image一般尺寸过大,不适用于所有界面。
Image一般尺寸较大,不适合用作icon(大图片用作icon时只显示图片的一部分),需要经过处理:
Icon是接口,一般不直接用。
ImageIcon用途最广泛。
Image用来绘制图片,ImageIcon用来添加到各类组件中,直接显示要转化为Image。
paintIcon
Icon会自动调用 public void paintIcon(Component c, Graphics g, int x, int y) 方法
图像等比例缩放显示在Label内
package aaa;
import java.awt.*;
import java.awt.image<