Java - Image VS ImageIcon

鲜于允晨
2023-12-01

1.
Image类属于java.awt;
ImageIcon属于javax.swing;

2.
两者可以互相切换–肯定是借助于它们的构造函数啦;
可查阅API:
http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html

JButton的API(可查阅利用ImageIcon的构造)
http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html
举例说明:

// Icon由图片文件形成
ImageIcon imageIcon = new ImageIcon("D:\\Fox DownLoad\\Editplus_CK_XP85\\5.png"); 
// 但这个图片太大不适合做Icon,***Image和ImageIcon的互相转换***
Image image = imageIcon.getImage();                         

//为把它缩小点,先要取出这个Icon的image ,然后缩放到合适的大小
Image smallImage = image.getScaledInstance(30,20,Image.SCALE_FAST);


//再由修改后的Image来生成合适的Icon;
//***Image对象创建ImageIcon对象,可查阅ImageIcon的构造方法***
ImageIcon smallIcon = new ImageIcon(smallImage);

//最后设置它为按钮的图片
JButton iconButton = new JButton(smallIcon);
 类似资料: