java 如何将图像添加到 JFrame 标题栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:46:26  来源:igfitidea点击:

How to add an image to a JFrame title bar?

javaimageswingiconsjframe

提问by DarRay

I want to add an image (small icon) to the javax.swing.JFrametitle bar.

我想在javax.swing.JFrame标题栏中添加一个图像(小图标)。

How can I do it?

我该怎么做?

回答by mre

Since JPaneldoesn't have a title bar, I'm assuming that you're referring to JFrame. That being said, use setIconImage(...).

由于JPanel没有标题栏,我假设您指的是JFrame. 话虽如此,使用setIconImage(...).



Here is an example of using setIconImages().

这是一个使用setIconImages().

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;
import java.util.*;

class FrameIcons {

    public static void main(String[] args) throws Exception {
        URL url16 = new URL("http://i.stack.imgur.com/m0KKu.png");
        URL url32 = new URL("http://i.stack.imgur.com/LVVMb.png");

        final List<Image> icons = new ArrayList<Image>();
        icons.add(ImageIO.read(url16));
        icons.add(ImageIO.read(url32));

        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JFrame f = new JFrame("Frame Icons");
                f.setIconImages(icons);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLocationByPlatform(true);
                f.setSize(200,100);
                f.setVisible(true);
            }
        });
    }
}

Frame using 16x16 icon

使用 16x16 图标的框架

enter image description here

在此处输入图片说明

Application tabbing using the 32x32 icon

使用 32x32 图标的应用程序选项卡

enter image description here

在此处输入图片说明