Java Swing 中使用的框架图标的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18224184/
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
Sizes of frame icons used in Swing
提问by Andrew Thompson
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
?
我们可以使用列表来初始化窗口图标Window.setIconImages(List<? extends Image>)
。中通常使用的不同大小的图标是JFrame
什么?
Code
代码
This code turns 64 different sized images (from 16x16, incrementing by 2) into icons for the list.
此代码将 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 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);
}
}
采纳答案by Andrew Thompson
Typical views for this Windows 7 based PC
这台基于 Windows 7 的 PC 的典型视图
Note: @bobbel reports the same sizes are used for Windows 10.
注意:@bobbel 报告 Windows 10 使用相同的大小。
Frame - 20x20
框架 - 20x20
Task Bar - 40x40 in task bar itself, hover app shows 20x20
任务栏 - 任务栏本身为 40x40,悬停应用显示 20x20
Windows+Tab - 20x20
Windows+Tab - 20x20
Alt+Tab - 40x40 in lower right, shrunken 20x20 in upper left.
Alt+Tab - 右下角为 40x40,左上角缩小为 20x20。
Task Manager - 20x20
任务管理器 - 20x20
回答by mKorbel
@mKorbel Huh.. did not realize there would be a difference there. Well, user/OS preferences rules over programmer expectation! ;)
@mKorbel Huh .. 没有意识到那里会有区别。好吧,用户/操作系统的偏好高于程序员的期望!;)
answer is only about Win8(ent, 64b ....)/WinXP(not mentioned, but setting are quite similair)
there are another options please see Bug or feature: Swing default gui font incorrect for Win6+by @kleopatra, etc
is possible to set 64x64 icon on desktop in win8
e.g. my setting (not advanced graphics personalizations, despite the fact that I'm Win2008/12 admin,
blablabla-
"leaving surealism with reversed color scheme, now only with Black & Orange Colors"-end blablabla
)
答案仅是关于 Win8(ent, 64b ....)/WinXP(未提及,但设置非常相似)
还有其他选项请参阅错误或功能:@kleopatra 等对 Win6+的Swing 默认 gui 字体不正确
可以在win8桌面上设置64x64图标
例如我的设置(不是高级图形个性化,尽管我是 Win2008/12 管理员,
blablabla-
“用反转的配色方案留下了确定性,现在只使用黑色和橙色”-end blablabla
)
- generating
- 生成
- standard setting (only unmarked Use small taskbar buttons)
- 标准设置(仅未标记的使用小任务栏按钮)
- your window on my screen
- 你在我屏幕上的窗口
回答by EthanB
It seems there are no Frame or Dock icons when run in Mac OS X 10.9 (Mavericks):
在 Mac OS X 10.9 (Mavericks) 中运行时似乎没有框架或 Dock 图标:
Also, no icon in Activity Monitor:
此外,活动监视器中没有图标:
回答by blackl?tus
Ubuntu 12.04 LTS
Ubuntu 12.04 LTS
The taskbar icon size can be changed between 32 and 64, but all the time uses the 32x32 icon. I have also recompiled the program, but keeps using the same icon.
任务栏图标大小可以在 32 到 64 之间更改,但始终使用 32x32 图标。我也重新编译了程序,但一直使用相同的图标。
Taskbar and window (there is not icon at windows).
任务栏和窗口(Windows 上没有图标)。
Alt + Tab
Alt + Tab
no icon at task manager
任务管理器没有图标
回答by bobbel
I've found out an interesting thing on Win10 (could be also true for Win7 and Win8, but I haven't tried it yet).
我在 Win10 上发现了一个有趣的事情(Win7 和 Win8 也可能如此,但我还没有尝试过)。
By default, Win10 will use image sizes of 20x20 (small) and 40x40 (big).
默认情况下,Win10 将使用 20x20(小)和 40x40(大)的图像大小。
So, what if you let start your image sizes at 22? It will use image sizes 30x30(small) and 40x40(big)!
那么,如果你让你的图像尺寸从22 开始呢?它将使用30x30(小)和40x40(大)图像尺寸!
Generating a whole table of it shows interesting behaviors (tests between tested start sizes result in previous sizes; so 4will also result in 20x20and 40x40):
生成它的整个表格显示了有趣的行为(测试起始尺寸之间的测试会产生以前的尺寸;所以4也会产生20x20和40x40):
If you start at 2, it will use 20x20and 40x40.
If you start at 22, it will use 30x30and 40x40.
If you start at 32, it will use for both 40x40.
If you start at 42, it will use for both 60x60.
If you start at 62, it will use 78x78and 80x80.
If you start at 80, it will use for both 80x80.
If you start at 82, it will use 98x98and 120x120.
If you start at 100, it will use 100x100and 120x120.
If you start at 102, it will use 118x118and 120x120.
If you start at 120, it will use for both 120x120.
If you start at 122, it will use 138x138and 158x158.
... okay this is enough ...
如果从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。
……好吧,这就够了……
I don't really get the pattern behind, but i found it very interesting...
我真的不明白背后的模式,但我发现它很有趣......
In the end it's really up to you, which sizes you provide. Every OS has it's own logic to show up specific icons. Either it will be scaled up or down if you don't provide the exact image sizes for each OS.
最后,这真的取决于您,您提供的尺寸。每个操作系统都有自己的逻辑来显示特定的图标。如果您没有为每个操作系统提供确切的图像大小,它将被放大或缩小。