- Swing 教程
- Swing - 首页
- Swing - 概述
- Swing - 环境
- Swing - 控件
- Swing - 事件处理
- Swing - 事件类
- Swing - 事件监听器
- Swing - 事件适配器
- Swing - 布局
- Swing - 菜单
- Swing - 容器
- Swing 有用资源
- Swing - 快速指南
- Swing - 有用资源
- Swing - 讨论
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) 从一个字节数组创建一个 ImageIcon,该数组从包含受支持图像格式(如 GIF、JPEG 或(从 1.3 版开始)PNG)的图像文件读取。 |
| 3 |
ImageIcon(byte[] imageData, String description) 从一个字节数组创建一个 ImageIcon,该数组从包含受支持图像格式(如 GIF、JPEG 或(从 1.3 版开始)PNG)的图像文件读取。 |
| 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() 返回此图标的 Image。 |
| 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 程序,例如在 D:/ > SWING > com > tutorialspoint > gui > 中。
SwingControlDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new SwingControlDemo();
swingControlDemo.showImageIconDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
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 = SwingControlDemo.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("/resources/java_icon.png","Java");
JLabel commentlabel = new JLabel("", icon,JLabel.CENTER);
controlPanel.add(commentlabel);
mainFrame.setVisible(true);
}
}
使用命令提示符编译程序。转到 D:/ > SWING 并输入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java
如果未出现错误,则表示编译成功。使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo
验证以下输出。