- Swing 编程示例
- 示例 - 主页
- 示例 - 环境设置
- 示例 - 边框
- 示例 - 按钮
- 示例 - 复选框
- 示例 - 组合框
- 示例 - 颜色选择器
- 示例 - 对话框
- 示例 - 编辑器面板
- 示例 - 文件选择器
- 示例 - 格式化文本字段
- 示例 - 框架
- 示例 - 列表
- 示例 - 布局
- 示例 - 菜单
- 示例 - 密码字段
- 示例 - 进度条
- 示例 - 滚动面板
- 示例 - 滑块
- 示例 - 计数器
- 示例 - 表格
- 示例 - 工具栏
- 示例 - 树
- Swing 实用资源
- Swing - 快速指南
- Swing - 实用资源
- Swing - 讨论
Swing 示例 - 更改窗口的默认图标
以下示例展示如何更改基于 Swing 的应用程序中的窗口的默认图标。
我们正在使用以下 API。
ImageIcon − 创建图像图标。
JFrame.setIconImage() − 设置窗口的图标。
示例
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SwingTester {
public static void main(String[] args) {
createWindow();
}
private static void createWindow() {
JFrame frame = new JFrame("Swing Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon arrowIcon = null;
java.net.URL imgURL = SwingTester.class.getResource("arrow.jpg");
if (imgURL != null) {
arrowIcon = new ImageIcon(imgURL);
frame.setIconImage(arrowIcon.getImage());
} else {
JOptionPane.showMessageDialog(frame, "Icon image not found.");
}
createUI(frame);
frame.setSize(560, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void createUI(JFrame frame){
JPanel panel = new JPanel();
LayoutManager layout = new FlowLayout();
panel.setLayout(layout);
panel.add(new JLabel("Hello World!"));
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
}
输出
swingexamples_frames.htm
广告