且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows/Linux 上设置 Java Swing 应用程序的 DPI?

更新时间:2023-10-16 23:21:16

我目前正在 Windows 上调查此问题.这是我发现的:

I'm currently investigating this issue on Windows. Here's what I found:

最摇摆的造型感觉根本不支持高 DPI,甚至不支持 Nimbus,即使它应该是可扩展的.我发现一些旧博客文章说 Nimbus 最终可能会提供高 DPI 缩放,但显然这从未发生过.

Most Swing Look & Feels don't support high DPI at all, not even Nimbus even though it's supposed to be scalable. I found some old blog posts saying that Nimbus might eventually offer high DPI scaling, but apparently that never happened.

一个例外是系统 LAF,但在所有 DPI 设置下,其默认字体比实际系统字体大小小约 10%.此外,必须按照此处所述明确选择系统:http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

The one exception is System LAF but its default font is ~10% smaller than the actual system font size, at all DPI settings. Moreover, System must be selected explicitly as described here: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

您也无法在 Swing 中设置单一的缩放因子.特定的 LAF 必须提供代码来处理缩放.所以你能做的***的事情就是选择系统并希望它足够好.

There's no single scaling factor that you could set in Swing, either. The specific LAF has to provide code to handle scaling. So the best you can do is select System and hope it's good enough.

但是,JavaFX 在我的系统上可以正确并自动一直扩展到 150%.如果可能的话,我建议您使用 JavaFX 来构建您的 GUI.

However, JavaFX does correctly and automatically scale all the way up to 150% on my system. If at all possible, I suggest you use JavaFX to build your GUI.

我制作了几个小测试程序,并对各种 GUI 框架、Swing 主题和 DPI 设置进行了比较截图.这可能对阅读此问题的人有用:http://kynosarges.org/GuiDpiScaling.html

edit: I made a couple small test programs and took comparison screenshots for various GUI frameworks, Swing themes, and DPI settings. This might be informative for people reading this question: http://kynosarges.org/GuiDpiScaling.html