Java 自动适应 Windows 7 字体大小调整
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2045811/
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
Java automatically adjusting to the Windows 7 font size adjustment
提问by Glenn
In Windows 7, if you change the font size via the Control Panel->Appearance and Personalization -> Display "Make text and other items larger or smaller", it adjusts not only the menu sizes, but also the text content size of apps like Notepad, Wordpad, Firefox.
在 Windows 7 中,如果您通过控制面板-> 外观和个性化 -> 显示“放大或缩小文本和其他项目”更改字体大小,它不仅会调整菜单大小,还会调整应用程序的文本内容大小,例如记事本、写字板、火狐。
Is there a way to get Java to automatically scale the font without having to manually scale it?
有没有办法让 Java 自动缩放字体而无需手动缩放?
回答by Joshua McKinnon
There are two parts to this:
这有两个部分:
- Getting your components, fonts, etc to scale
- Getting your layouts to scale
- 缩放组件、字体等
- 使您的布局按比例缩放
For Swing, the first part is easy - it all starts with one call.
对于 Swing 来说,第一部分很简单——一切都从一个调用开始。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
On Windows, this will cause it to respect your Small/Large fonts (DPI) setting.
在 Windows 上,这将使其尊重您的小/大字体 (DPI) 设置。
Here are two screenshots from a quick test app I threw together, demonstrating how it looks on my machine in Windows 7 @ 96dpi (normal font size) and @ 144dpi (150%)
这是我拼凑的一个快速测试应用程序的两个屏幕截图,展示了它在 Windows 7 @ 96dpi(正常字体大小)和 @ 144dpi(150%)下在我的机器上的外观
First the default font size sample:
首先是默认字体大小示例:
Now with Larger (150%) font size set:
现在设置更大 (150%) 字体大小:
There is no code change between runs, only logging out & back in with new DPI settings. I set a fixed frame size on purpose to demonstrate that my container is not scaling in size, which caused my label to get pushed down in order to fit.
运行之间没有代码更改,仅注销并使用新的 DPI 设置重新登录。我特意设置了一个固定的框架大小,以证明我的容器没有按比例缩放,这导致我的标签被压下以适应。
Here is my source code - cut & paste and run it yourself:
这是我的源代码 - 剪切并粘贴并自己运行:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class SwingFontTest
{
private static void createGUI()
{
JButton button = new JButton("my button with Some Text");
JLabel label = new JLabel("and a label");
JPanel panel = new JPanel(new FlowLayout());
panel.add(button);
panel.add(label);
JFrame frame = new JFrame("Title!");
frame.setContentPane(panel);
frame.setSize(300,125);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGUI();
}
});
}
}
The Look & Feel supplies the default sizing, but it is up to the GUI author to use scalable units in their layouts. It does take effort (scalable layouts are a pain on webpages too!), but it's definitely attainable.
外观和感觉提供默认大小,但由 GUI 作者在其布局中使用可缩放单元。这确实需要努力(可伸缩的布局对网页来说也是一种痛苦!),但它绝对是可以实现的。
I recommend using a layout like FormLayoutthat will let you define your layouts in Dialog Units (DLU), as these scale with DPI. That will enable you to make your containers scale in size and should help limit behaviors like the label moving to the next line due to sizing. If the size of the frame was determined using dialog units then it could be made to look the same, only larger.
我建议使用像FormLayout这样的布局,它可以让您在 Dialog Units (DLU) 中定义布局,因为它们随 DPI 缩放。这将使您能够扩大容器的尺寸,并有助于限制标签因尺寸而移动到下一行等行为。如果框架的大小是使用对话单元确定的,那么它可以看起来相同,只是更大。
It is late - so that's it for now.
已经晚了 - 现在就这样。
回答by Jherico
This would depend entirely on what GUI toolkit you're using. I'm almost certain that SWT for instance would automatically pick up the new sizes (since it uses native widgets). AWT might as well for all I know. Can you specify what what toolkit you're using and whether you've determined that its not already doing what you want (and possibly how)?
这完全取决于您使用的 GUI 工具包。我几乎可以肯定,例如 SWT 会自动选择新的尺寸(因为它使用本机小部件)。据我所知,AWT 也可以。您能否指定您使用的是什么工具包,以及您是否已经确定它没有做您想要的(以及可能如何)?