Can I set the DPI resolution of my Java Swing application without changing the systems' DPI setting?

前端 未结 3 1950
忘掉有多难
忘掉有多难 2021-02-07 21:21

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 chang

相关标签:
3条回答
  • 2021-02-07 21:43

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

    0 讨论(0)
  • 2021-02-07 21:50

    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

    0 讨论(0)
  • 2021-02-07 21:51

    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.

    0 讨论(0)
提交回复
热议问题