Swing中使用的框架图标尺寸

33

我们可以使用列表来初始化窗口图标,使用Window.setIconImages(List<? extends Image>)方法。在JFrame中通常使用什么不同大小的图标?

代码

此代码将64个不同大小的图像(从16x16开始,每次增加2)转换为列表中的图标。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FrameIconList {

    public static BufferedImage getImage(int size, Color color) {
        BufferedImage i = new BufferedImage(
                size, size, BufferedImage.TYPE_INT_RGB);

        Graphics2D g = i.createGraphics();

        g.setColor(color);
        g.fillRect(0, 0, size, size);
        g.setColor(Color.BLACK);
        int off = (size>17 ? 3 : 1);
        if (off>1) g.drawRect(0, 0, size-1, size-1);
        g.drawString("" + size, off, size-off);

        g.dispose();

        return i;
    }

    public static void main(String[] args) {
        final Color[] colors = {
            Color.GREEN,
            Color.RED,
            Color.YELLOW,
            Color.WHITE,
            Color.CYAN,
            Color.MAGENTA,
            Color.PINK,
            Color.ORANGE
        };

        int s = 64;
        final int[] sizes = new int[s];
        
        for (int ii=0; ii<sizes.length; ii++) {
            sizes[ii] = 16+(ii*2);
        }

        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));
                gui.setBackground(Color.WHITE);
                
                ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
                Vector<ImageIcon> icons = new Vector<ImageIcon>();
                for (int ii=0; ii< sizes.length; ii++) {
                    BufferedImage bi = getImage(
                            sizes[ii], 
                            colors[ii%colors.length]);
                    images.add(bi);
                    ImageIcon imi = new ImageIcon(bi);
                    icons.add(imi);
                }
                JList list = new JList(icons);
                list.setVisibleRowCount(6);
                gui.add(new JScrollPane(list));

                JFrame f = new JFrame("Icon size usage");
                f.setIconImages(images);
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://dev59.com/7Ww05IYBdhLWcg3wmC6_#7143398 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
6个回答

34

这个基于Windows 7的电脑的典型视图

注意:@bobbel报告称,在Windows 10中使用相同的尺寸。

框架 - 20x20

图标尺寸用法-显示20x20帧图标

任务栏 - 任务栏本身40x40,悬停应用程序显示20x20

图标尺寸用法-任务栏

Windows+Tab - 20x20

图标尺寸用法-Windows+Tab

Alt+Tab - 右下角40x40,左上角缩小20x20。

图标尺寸用法-Alt+Tab

任务管理器 - 20x20

图标尺寸用法-任务管理器


5
刚刚测试了一下,在Win10中所有的尺寸都是一样的。 - bobbel
这可能是因为显示比例设置为125%。当没有缩放效果(100%缩放)时,Windows在应用程序框架中使用16×16图标,在Alt + Tab中使用32×32图标。这与16 * 1.25 = 20,32 * 1.25 = 40相一致。如果在显示设置中将比例设置为150%,则图标将变大:分别为24和48。 - Alexey Ivanov

13

@mKorbel 哦.. 没想到那里会有差别。嗯,用户/操作系统的偏好胜过程序员的期望!;)

  • 本答案仅适用于Win8企业版、64位.../WinXP(未提及,但设置相当类似)

  • 还有其他选项,请参见Bug or feature: Swing default gui font incorrect for Win6+ by @kleopatra等

  • 在win8中可以设置桌面上的64x64图标

  • 例如我的设置(不是高级图形个性化,尽管我是Win2008/12管理员,“blablabla-离开超现实主义,采用反色方案,现在仅使用黑色和橙色”,结束blablabla)

enter image description here

  • 生成中

enter image description here

  • 标准设置(只取消标记“使用小任务栏按钮”)

enter image description here

  • 你的窗口在我的屏幕上

enter image description here


12

在 Mac OS X 10.9(Mavericks)上运行时似乎没有框架或停靠栏图标:

Mac OS X 10.9(Mavericks)中缺少 Java Swing 图标

此外,在活动监视器中也没有图标:

输入图像描述


2
在Mac上部署Java应用程序并不像在Windows上那样轻松,使其感觉原生。最初的步骤是在.app目录中包装您的Java应用程序,并在其中放置一个Info.plist描述符文件,该文件反过来指向可用于应用程序的图标。请参见这里 - Timmos
1
@EthanB,我找到的唯一解决方案是 java -Xdock:name="My App" -Xdock:icon="/path/to/myicon.icns" - tresf

11

Ubuntu 12.04 LTS

任务栏图标大小可在32和64之间更改,但始终使用32x32大小的图标。我已重新编译了程序,但仍然使用相同的图标。

任务栏和窗口(窗口中没有图标)。

在此输入图片描述

Alt + Tab

在此输入图片描述

任务管理器中没有图标


3
以下是我的在 Windows 10 上的结果,取决于屏幕缩放比例:
在 100% 时: 框架 - 16x16 任务栏 - 32x32
在 125% 时: 框架 - 20x20 任务栏 - 40x40
在 150% 时: 框架 - 24x24 任务栏 - 48x48
在 175% 时: 框架 - 28x28 任务栏 - 56x56
在 200% 时: 框架 - 32x32 任务栏 - 64x64

这个问题已经有6.5年的历史,已经有很多答案了,其中一个已被采纳。 - Pentium1080Ti
3
是的,采纳的答案并不正确关于 Windows 10。 - Jake S
2
我也发现了同样的事情。Windows 10(2021年7月)使用所有这些尺寸的图标(取决于不同的缩放设置):16、20、24、28、32、40、56、64像素。如果您没有提供精确的图标尺寸,Windows 将缩放它找到的任何图标(虽然它做得不是很好)。 - ᴇʟᴇvᴀтᴇ

2
我在Win10上发现了一件有趣的事情(在Win7和Win8上也可能适用,但我还没有尝试过)。
默认情况下,Win10将使用20x20(小)和40x40(大)的图像大小。
那么,如果您让您的图像大小从22开始呢?它将使用图像大小30x30(小)和40x40(大)!
生成整个表格显示出有趣的行为(在测试的起始大小之间进行测试会导致先前的大小;因此,4也将导致20x20和40x40):
如果您从2开始,它将使用20x20和40x40。 如果您从22开始,它将使用30x30和40x40。 如果您从32开始,它将同时使用40x40。 如果您从42开始,它将同时使用60x60。 如果您从62开始,它将使用78x78和80x80。 如果您从80开始,它将同时使用80x80。 如果您从82开始,它将使用98x98和120x120。 如果您从100开始,它将使用100x100和120x120。 如果您从102开始,它将使用118x118和120x120。 如果您从120开始,它将同时使用120x120。 如果您从122开始,它将使用138x138和158x158。 ......好吧,这就够了......
我真的不明白背后的模式,但我觉得它非常有趣......
最终,您提供的大小真的取决于您。每个操作系统都有自己的逻辑来显示特定的图标。如果您没有为每个操作系统提供精确的图像大小,则会缩放上或下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接