Java自动适应Windows 7字体大小调整
在Windows 7中,如果您通过控制面板->外观和个性化->更改字体大小显示“放大或缩小文本和其他项目”,它不仅可以调整菜单大小,还可以调整记事本、写字板、Firefox 等应用程序的文本内容大小。
有没有办法让Java自动缩放字体而无需手动缩放?
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.
Is there a way to get Java to automatically scale the font without having to manually scale it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有两个部分:
缩放
对于Swing
,第一部分很简单 - 一切都从一次调用开始。
在 Windows 上,这将导致它尊重您的小/大字体 (DPI) 设置。
以下是我整理的快速测试应用程序的两个屏幕截图,演示了它在 Windows 7 @ 96dpi(正常字体大小)和 @ 144dpi(150%)中的机器上的外观。
首先是默认字体大小示例:
现在设置更大 (150%) 字体大小:
运行之间没有代码更改,仅注销和退出使用新的 DPI 设置返回。我故意设置了一个固定的框架尺寸,以证明我的容器尺寸没有缩放,这导致我的标签被推下以适应。
这是我的源代码 - cut &自己粘贴并运行:
The Look & Feel 提供默认大小,但由 GUI 作者在其布局中使用可缩放单位。这确实需要付出努力(可扩展布局在网页上也是一种痛苦!),但它绝对是可以实现的。
我建议使用像 FormLayout 这样的布局,它可以让您在对话框单元 (DLU ),因为它们随 DPI 缩放。这将使您能够缩放容器的尺寸,并有助于限制标签因尺寸调整而移动到下一行等行为。如果框架的大小是使用对话单元确定的,那么它可以看起来相同,只是更大。
已经很晚了——所以现在就这样。
There are two parts to this:
to scale
scale
For Swing, the first part is easy - it all starts with one call.
On Windows, this will cause it to respect your Small/Large fonts (DPI) setting.
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%)
First the default font size sample:
Now with Larger (150%) font size set:
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.
Here is my source code - cut & paste and run it yourself:
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.
I recommend using a layout like FormLayout that 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.
It is late - so that's it for now.
这完全取决于您使用的 GUI 工具包。我几乎可以肯定 SWT 会自动选择新的尺寸(因为它使用本机小部件)。据我所知,AWT 也可能是这样。您能否指定您正在使用什么工具包以及您是否确定它尚未执行您想要的操作(以及可能如何执行)?
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)?