我可以在不更改系统的情况下设置 Java Swing 应用程序的 DPI 分辨率吗? DPI 设置?

发布于 2024-07-26 23:06:02 字数 280 浏览 6 评论 0原文

我有一个使用 Substance LookAndFeel 并以 Windows 作为目标平台的 Java 应用程序,我想在不更改系统设置的情况下增加应用程序的 DPI 设置。

我想这样做是因为我不想强迫用户重新启动 Windows 并且因为许多 Windows 应用程序似乎在非常高的 DPI 设置(> 120)下出现问题

PS:我知道物质 LaF 允许缩放运行时的字体大小,但这样只缩放控件的高度,而不缩放宽度。 我希望我的 GUI 完全缩放,因为如果我设置系统的 DPI 设置,就会发生这种情况。

I have a Java application using the Substance LookAndFeel with Windows as the the target platform and I want to increase the DPI setting of my application without changing the system setting.

I want to do this because I don't want to force the user to restart Windows and because many Windows applications seem to have problems with very high DPI settings (> 120)

PS: I'm aware that the Substance LaF allows to scale the font size at runtime, but that way only the height of my controls are scaled, not the width. I want my GUI fully scaled as it would happen if I set the system's DPI setting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你穿错了嫁妆 2024-08-02 23:06:02

不知道这是否可能。 外观和感觉必须支持它,但据我所知,Windows 外观和感觉不支持它。 您可以考虑以下技巧:迭代外观和感觉中定义的所有字体,并将它们重新定义为稍大一些。 这是执行此操作的代码片段:

for (Iterator i = UIManager.getLookAndFeelDefaults().keySet().iterator(); i.hasNext();) {
    String key = (String) i.next();
    if(key.endsWith(".font")) {
        Font font = UIManager.getFont(key);
        Font biggerFont = font.deriveFont(2.0f*font.getSize2D());
        // change ui default to bigger font
        UIManager.put(key,biggerFont);
    }
}

我想您可以更进一步并按比例重新定义比例边框,但这很快就会变得非常复杂

Don't know if that is possible. The look&feel would have to support it, and as far as I know, the Windows Look&Feel does not. Here's a hack which you may consider: Iterate through all the fonts defined in your look&feel and redefine them to be slighly bigger. Here is a code snippet that does this:

for (Iterator i = UIManager.getLookAndFeelDefaults().keySet().iterator(); i.hasNext();) {
    String key = (String) i.next();
    if(key.endsWith(".font")) {
        Font font = UIManager.getFont(key);
        Font biggerFont = font.deriveFont(2.0f*font.getSize2D());
        // change ui default to bigger font
        UIManager.put(key,biggerFont);
    }
}

I suppose you could take this one step further and redefine scale borders proportionally as well, but that gets very complicated very quickly

倾城泪 2024-08-02 23:06:02

所以实际的答案似乎是:不,你不能。 这真的很糟糕,因为测试起来很痛苦。

So the actual answer seems to be: no you can't. That really is a bummer because it's a pain to test.

皓月长歌 2024-08-02 23:06:02

是的,您可以,但您需要在 JRE 9 上运行它。

这是因为 Java 运行时声明自己是“DPI 感知的”,但实际上并不支持 AWT 和 Swing。 Java 应用程序的大小和渲染基于像素,而不是正确缩放,这包括 HiDPI 显示器。
不管怎样,这个问题最近已经解决了。
请参阅问题JEP 263:Windows 和 Linux 上的 HiDPI 图形
以及升级

所以,增加字体大小不起作用(因为它不会增加其余的东西); jvm 参数 -Dsun.java2d.dpiaware=false 不起作用(因为它并未得到真正支持); 并且清单文件+注册表编辑(对于Windows)不起作用。

解决方案:您需要在JRE 9上运行它,因为它确实支持此功能。

Yes you can, but you need to run it on JRE 9.

This is because the Java runtime declared itself to be "DPI-aware" but didn't really supported it for AWT and Swing. Java applications were sized and rendered based on pixels rather than being properly scaled, this included HiDPI displays.
Anyways, this has been recently solved.
See the issue JEP 263: HiDPI Graphics on Windows and Linux
and the upgrade.

So, increasing the font size does not work (because it does not increase the rest of the things); the jvm argument -Dsun.java2d.dpiaware=false does not work (because it was not really supported); and the manifest file + registry edit (for Windows) just does not work.

Solution: You need to run it on JRE 9 because it really supports this feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文