橘子味的心
标题:Swing ImageIcon

ImageIcon类是Icon接口的一个实现,它使用图片来绘制图标。

类声明

以下是javax.swing.ImageIcon类的声明 -

public class ImageIcon
   extends Object
      implements Icon, Serializable, Accessible

字段

以下是javax.swing.ImageIcon类的字段 -

  • protected static Component component
  • protected static MediaTracker tracker

类构造函数

编号 构造函数 描述
1 ImageIcon() 创建未初始化的图像图标。
2 ImageIcon(byte[] imageData) 从包含支持的图像格式的图像文件(例如GIF,JPEG或(从1.3开始)PNG)读取的字节数组创建ImageIcon
3 ImageIcon(byte[] imageData, String description) 从包含支持的图像格式的图像文件(例如GIF,JPEG或(从1.3开始)PNG)读取的字节数组创建ImageIcon
4 ImageIcon(Image image) 从图像对象创建ImageIcon
5 ImageIcon(Image image, String description) 从图像创建ImageIcon
6 ImageIcon(String filename) 从指定的文件创建ImageIcon
7 ImageIcon(String filename, String description) 从指定的文件创建ImageIcon。
8 ImageIcon(URL location) 从指定的URL创建ImageIcon
9 ImageIcon(URL location, String description) 从指定的URL创建ImageIcon

类方法

编号 方法 描述
1 AccessibleContext getAccessibleContext() 获取与此ImageIcon关联的AccessibleContext
2 String getDescription() 获取图像的描述。
3 int getIconHeight() 获取图标的高度。
4 int getIconWidth() 获取图标的宽度。
5 Image getImage() 返回此图标的图像。
6 int getImageLoadStatus() 返回图像加载操作的状态。
7 ImageObserver getImageObserver() 返回图像的图像观察者。
8 protected void loadImage(Image image) 加载图像,仅在加载图像时返回。
9 void paintIcon(Component c, Graphics g, int x, int y) 绘制图标。
10 void setDescription(String description) 设置图像的描述。
11 void setImage(Image image) 设置此图标显示的图像。
12 void setImageObserver(ImageObserver observer) 设置图像的图像观察者。
13 String toString() 返回此图像的字符串表示形式。

方法继承

该类继承以下类中的方法 -

  • java.lang.Object

ImageIcon示例

使用编辑器创建以下Java程序 -


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageIconExample {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public ImageIconExample(){
      prepareGUI();
   }
   public static void main(String[] args){
       ImageIconExample  swingControlDemo = new ImageIconExample();      
      swingControlDemo.showImageIconDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing ImageIcon 示例");
      mainFrame.setSize(500,500);
      mainFrame.setLayout(new GridLayout(3, 1));

      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   // Returns an ImageIcon, or null if the path was invalid. 
   private static ImageIcon createImageIcon(String path,
      String description) {
      java.net.URL imgURL = ImageIconExample.class.getResource(path);

      if (imgURL != null) {
         return new ImageIcon(imgURL, description);
      } else {            
         System.err.println("Couldn't find file: " + path);
         return null;
      }
   }
   private void showImageIconDemo(){
      headerLabel.setText("Control in action: ImageIcon"); 
      ImageIcon icon = createImageIcon("images/java.jpg","Java");

      JLabel commentlabel = new JLabel("", icon,JLabel.CENTER);
      controlPanel.add(commentlabel);
      mainFrame.setVisible(true);  
   }
}

执行上面示例代码,得到以下结果:


目录

分类