Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

java - Sizes of frame icons used in Swing

We can use a list to initialise the window icons using Window.setIconImages(List<? extends Image>). What are the different sizes of icons typically used for in a JFrame?

Code

This code turns 64 different sized images (from 16x16, incrementing by 2) into icons for the list.

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 http://stackoverflow.com/a/7143398/418556 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);
    }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typical views for this Windows 7 based PC

Note: @bobbel reports the same sizes are used for Windows 10.

Frame - 20x20

Icon Size Usage - showing 20x20 frame icon

Task Bar - 40x40 in task bar itself, hover app shows 20x20

Icon Size Usage - Task Bar

Windows+Tab - 20x20

Icon Size Usage - Windows+Tab

Alt+Tab - 40x40 in lower right, shrunken 20x20 in upper left.

Icon Size Usage - Alt+Tab

Task Manager - 20x20

Icon Size Usage - Task Manager


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...