java窗口图标怎么换_如何更改Swing窗口的默认图标?

下面的示例展示了如何在基于Swing的应用程序中更改窗口的默认图标。

使用以下API -

ImageIcon - 创建图像图标。

JFrame.setIconImage() - 将图标设置为窗口。

示例

package com.yiibai.swingdemo;

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窗口的默认图标");

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("Welcome to Yiibai.com!"));

frame.getContentPane().add(panel, BorderLayout.CENTER);

}

}

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

dd433d30c89deff703b48e3560f1278a.png

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值