簡體   English   中英

如何將圖像添加到 JFrame 標題欄?

[英]How to add an image to a JFrame title bar?

我想向javax.swing.JFrame標題欄添加一個圖像(小圖標)。

我該怎么做?

由於JPanel沒有標題欄,我假設您指的是JFrame 話雖如此,請使用setIconImage(...)


這是使用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);
            }
        });
    }
}

使用 16x16 圖標的框架

在此處輸入圖片說明

使用 32x32 圖標的應用程序選項卡

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM