Java Swing JFrame size different after upgrade to JRE11 from JRE 7 or 8. How can I make the frame size consistent?

huangapple go评论76阅读模式
英文:

Java Swing JFrame size different after upgrade to JRE11 from JRE 7 or 8. How can I make the frame size consistent?

问题

我协助维护一个大型且非常古老的Swing项目。我们注意到在JRE 11下运行的Swing项目页面与在我们先前版本的JRE 8下运行的项目页面有相当大的不同之处。

我一直在探索这个问题。我注意到一个问题是基本的大小似乎不同。JRE 11使所有的JFrames都变大了。我们需要保持大小的一致性。

这里有一个小程序来演示我所看到的情况:

import javax.swing.*;

public class JustAFrame
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(
                () -> {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setSize(400, 500);
                    frame.setVisible(true);
                });
    }
}

我在同一台运行Windows 10的机器上使用了java 64位版本jdk1.8.0_241和zulu11.41.23-ca-fx-jdk11.0.8-win_x64来运行这个程序。我附上了一张图像,将两个java版本的运行结果并排显示。JRE 11版本似乎要大约增大了50%。

我该怎么做才能让JRE 11的JVM使我们的JFrames的大小与我们先前版本的java相同,而不必进入我们应用程序中的每个JFrame实例并调整大小的Dimension参数呢?

英文:

I help maintain a large, very old Swing project. We noticed that the swing project pages running under JRE 11 look quite a bit different from the swing project running under JRE 8, our previous version.

I have been exploring this issue. One thing I noticed is that basic sizing seems to be different. JRE11 makes all the JFrames larger. We need to have the sizing stay consistent.

Here is a small program to demonstrate what I am seeing:

    import javax.swing.*;

public class JustAFrame
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(
                () -> {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
                    frame.pack();
                    frame.setSize(400, 500);
                    frame.setVisible(true);
                });
    }
}

I ran this program on the same Windows 10 machine using java 64 bit version jdk1.8.0_241 and
zulu11.41.23-ca-fx-jdk11.0.8-win_x64.
I'm attaching an image showing the same class running in the two java versions side by side. The JRE 11 version appears to be about 50% larger.
What can I do to make the JRE 11 JVM make our JFrames the size as our previous version of java, without having to go into every JFrame instance in our application and fiddling with the size Dimension parameter?

Java Swing JFrame size different after upgrade to JRE11 from JRE 7 or 8. How can I make the frame size consistent?

答案1

得分: 2

这是因为在Java 9之后,Windows的缩放兼容性得到了正确的处理。这意味着如果您的显示器设置为150%的缩放,Java应用程序的缩放也将达到150%。要禁用此功能,可以使用以下方法(在我的zulu11中有效):

设置VM选项:-Dsun.java2d.uiScale=1.0

或者

System.setProperty("sun.java2d.uiScale", "1.0");

其中1.0等于100%。

英文:

This is due to after Java 9, windows scaling compatibility working correctly.
that means if your monitor is set to scale at 150%, the Java application is scaled to 150%. to disable this, use the following methods (worked for me in zulu11)

set VM option : -Dsun.java2d.uiScale=1.0

OR

System.setProperty("sun.java2d.uiScale", "1.0");

1.0 = 100%

huangapple
  • 本文由 发表于 2020年10月23日 03:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64489140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定