本文介绍了Java Swing中的HiDPI支持可实现多种外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为某些Swing应用程序添加Hi-DPI支持,但是我找不到能满足我需求的解决方案.我需要支持多种外观感觉,所以情况似乎比我发现的其他帖子要复杂得多(这些帖子通常会建议调整您的UI大小以匹配您的字体大小").

一些实验发现,UIManager包含许多指标,可以对其进行调整,以使您在使应用程序对Hi-DPI友好的过程中获得一个良好的开端. ( UIManager-Defaults 实用程序对于探索这些功能非常有用!)但是我发现,L& F似乎在完全上彼此完全不同地工作:

  • Windows L& F为您提供了很好的(不是完美的)默认字体大小,并且内置图标(如复选框和窗口图标)的大小也适当调整-但许多其他指标仍然不可行. /p>

  • 在Metal中,您可以分别更新UIManager中的字体.通过一些工作,您可以缩放内置的IconUIResource使其匹配.

  • 在Nimbus中,您只可以更新一个默认字体,其他字体就位...但是如何缩放内置图标并具有组合框,单选按钮(等等),这超出了我的范围.渲染成功!

我从玩耍中得到的感觉是,应该有可能为每个L& F特定地创建一个特定的调整列表.这可能包括调整FontIconIntegerDimension的默认值.

有人对此提出好的解决方案吗?

任何人都可以共享需要针对标准L& F进行调整的UIDefaults的明确列表吗?

我对始终支持Metal和Windows的解决方案感到满意.

我想这样的解决方案应该是可重用的&;可以为一系列Swing应用程序解决相同的问题.我很惊讶似乎还没有这样的实用程序. (如果不能,请赐教!)这种方法当然不能解决所有问题(例如,您仍然需要手动缩放对setPreferredSize等的任何调用.然后,已经支持多种L& F的应用程序应该避免使用仍然可以调用它.)不过,我认为它可以使许多应用程序有个良好的开端.

我知道 JDK-9 承诺完整的 Hi-DPI支持,但我不能等那么久-即使在2017年发布之后,我也可能无法切换一段时间.

解决方案

  • 不是答案,仅是我对这个问题的见解,请不要删除此答案,这对于接下来的一两年非常有用.

  • 我的观点(我仅限于懒惰的Win用户,这可能对LI/(U)NIX或OSX的真正高级用户的体验很有趣)

    1. 倾向于使用NullLayout返回,尽管UIManager(我认为这仍然是事实)可以使用getPreferredSize 21k x 48k进行处理,但是LayoutManager似乎不知道将它们分开值正确,您可以看到,使用GridBagLayout可以看到此问题,对于4k场景,scaling/zoom_in确实存在问题,我认为AWT/Swing GUI在2k监视器上呈现正确性,

    2. 对于2k/4k屏幕,您必须覆盖UIManager中的所有键(例如,用于Nimbus L& F的AFAIK可以直接使用存储在xml文件中的结构)

      • 必须创建自己的paintIcon(在AWT/Swing中使用简单的形状)
      • 覆盖边界
      • 覆盖鼠标事件
      • 覆盖焦点事件
    3. 在今天的应用中,平滑的GUI"也必须部分地执行这些步骤,从旧的4:3屏幕->带有低分辨率小屏幕的笔记本电脑(HD_screens)->全高清屏幕->全屏显示,结束屏幕为2k时,请注意,我从未尝试过使用Nimbus L& F中内置的上浆varians,专业应用程序必须包含测试,检查

      • 本地操作系统(字体,边框,大小均不同)
      • 通过具有屏幕缩放功能的主显示屏获得以像素为单位的屏幕分辨率,例如,尤其是FullHd屏幕(16:9)和FullHd宽屏(21:9)
      • ,当您有多台显示器并且像素分辨率不同时,可以缓存所有显示(问题是在有两个相同的显示器的情况下,但是设置不合理-用户对GPU的设置,或显示器具有活动的电视卡-可以通过电视芯片修改设置)

      • 然后可以使用硬编码矩阵创建GUI以进行各种大小调整,以适应所有屏幕标准,默认情况下,成功地通过覆盖(父)容器的getPreferredSize成功,然后LayoutManager将接受getPreferredSize形式的硬编码矩阵作为原型并正确地做自己的工作,

    4. 如果我还记得其他有趣的事实,错误以及其他内容,...等等,我将用我的言论编辑这个较长的评论

I'm looking to add Hi-DPI support to some Swing applications, but I have failed to find a solution sufficient to my needs. I need to support multiple look & feels, so the situation seems rather more complex than other posts I've found (which tend to suggest "adapt your UI sizes to match your font size").

Some experimentation has found the UIManager contains many metrics that can be tweaked to get you off to a good start in making an application Hi-DPI friendly. (The UIManager-Defaults utility has been invaluable for exploring these!) What I found though is that the L&Fs seem to work completely differently to each other:

  • Windows L&F gives you a good (not perfect) default font size, and built-in icons (like checkbox & window icons) are sized appropriately - but many other metrics are still out of whack.

  • In Metal you can update the fonts in the UIManager individually. With a bit of work you can scale the built-in IconUIResources to match.

  • In Nimbus you can just update a single default font, and the other fonts fall into place... but it's beyond me how to scale the built-in icons and have combo-boxes, radio buttons (etc) render successfully!

The feeling I get from playing around is that it should be possible to create a list of specific tweaks for each L&F-specific independently. This would comprise potentially tweaking the defaults for Font, Icon, Integer and Dimensions.

Has anyone come up with a good solution to this?

Can anyone share a definitive list of which UIDefaults need adjustment for the standard L&Fs?

I would be happy with a solution that just supports Metal and Windows consistently.

I imagine such a solution should be quite reusable & could solve the same problem for a range of Swing apps. I'm surprised no such utility yet seems to exist. (Please enlighten me if not!) This approach won't solve everything of course (for example you'd still need to manually scale any calls to setPreferredSize etc. Then again apps that already support mulitple L&Fs should tend to avoid calling that anyway.) Still, I imagine that it could get many apps off to a good start.

I'm aware that JDK-9 promises full Hi-DPI support, but I cannot wait that long - I may not be able to switch for some time even after it's 2017 release.

解决方案

  • not an answer, just my insights to this problematics issue, and please don't delete this answer, it can be very informative for the next one, two year,

  • my points, (I'm restrict to lazy Win user, could be interesting the experiences by real power users from LI/(U)NIX or OSX)

    1. tend to returns back use NullLayout, despite the fact that the UIManager (I think that this is still a true) is able handling with getPreferredSize 21k x 48k, but seems like as LayoutManager doesn't know to divide those value properly, you can see it, this issue you can see by using GridBagLayout, there is reall issue with scaling/zoom_in for 4k sceens, I think that AWT/Swing GUI is rendered correclty at 2k monitors,

    2. for 2k/4k screens you have to overide all Keys in UIManager (e.g. AFAIK for Nimbus L&F is possible to use structure stored in xml file directly)

      • have to create own paintIcon (in AWT/Swing are used simple shapes)
      • override Borders
      • override mouse events
      • override Focus events
    3. those steps are partially required for "smooth GUI" in todays aplications too, from old 4:3 screens -> thought laptops with small_screens with low resolutione (HD_screens) -> fullhd screen -> fullhs wide sceen, ends with 2k screen, note I'm never tried by using the sizing varians builts in Nimbus L&F, profesional application must contains test, checking for

      • Native OS (there is difference betweens font, border, sizing)
      • get a screen resolution in pixels from primary display with screen scalling, e.g., especially FullHd screen (16:9) and wide FullHd screen (21:9)
      • to cache all displays in the case that you have the multiple monitors and resolution in pixels is different (problem is in the case that there are two the same monitors, but there is discreasing setting - users settings to GPU, or monitor has active TV card - settings can be modified by TV chip)

      • then GUI can be created with hardcoded matrix for various sizing, to fits all screen standards, by default with success by override getPreferredSize for (parent) container(s), then LayoutManager will accept getPreferredSize form hardcoded matrix as prototype and do own job correctly,

    4. if I remember another interesting facts, bugs, and whatever, ... etc I'll edit this my longer comment, with my remarks

这篇关于Java Swing中的HiDPI支持可实现多种外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:55