为什么 JFrame pack() 会影响位置?

发布于 2025-01-20 13:09:58 字数 1422 浏览 2 评论 0原文

我查看了 不可调整大小的 JFrame 更改 pack() 上的位置 和另一个。我的情况要简单得多,而且有所不同。代码如下:

public static void main_TestLoc()
{
    System.out.println("         Java Version: " + System.getProperty("java.version").toString());
    System.out.println("      Java VM Version: " + System.getProperty("java.vm.version").toString());
    System.out.println("Java  Runtime Version: " + System.getProperty("java.runtime.version").toString());
    System.out.println("          OS Platform: " + System.getProperty("os.name"));
    
    System.out.println();
    
    JFrame displayFrame = new JFrame();
    
    displayFrame.setLocation(200, 200);
    
    System.out.printf("Loc Before: (%4d, %4d)\n",
            displayFrame.getLocation().x,
            displayFrame.getLocation().y);
    
    displayFrame.pack();
    
    System.out.printf(" Loc After: (%4d, %4d)\n",
            displayFrame.getLocation().x,
            displayFrame.getLocation().y);

    displayFrame.setVisible(true);
    
    System.exit(0);
}

输出如下:

         Java Version: 17.0.2
      Java VM Version: 17.0.2+8
Java  Runtime Version: 17.0.2+8
          OS Platform: Mac OS X

Loc Before: ( 200,  200)
 Loc After: ( 200,  173)

问题是 pack() 应该改变 Frame 的大小;为什么它会改变位置?以及如何阻止它这样做?注意:当我将组件放入框架中时, pack() 的这种行为不会改变。

I have looked at Not resizable JFrame changes location on pack() and one other. My situation is much simpler and different. The code is below:

public static void main_TestLoc()
{
    System.out.println("         Java Version: " + System.getProperty("java.version").toString());
    System.out.println("      Java VM Version: " + System.getProperty("java.vm.version").toString());
    System.out.println("Java  Runtime Version: " + System.getProperty("java.runtime.version").toString());
    System.out.println("          OS Platform: " + System.getProperty("os.name"));
    
    System.out.println();
    
    JFrame displayFrame = new JFrame();
    
    displayFrame.setLocation(200, 200);
    
    System.out.printf("Loc Before: (%4d, %4d)\n",
            displayFrame.getLocation().x,
            displayFrame.getLocation().y);
    
    displayFrame.pack();
    
    System.out.printf(" Loc After: (%4d, %4d)\n",
            displayFrame.getLocation().x,
            displayFrame.getLocation().y);

    displayFrame.setVisible(true);
    
    System.exit(0);
}

The output is below:

         Java Version: 17.0.2
      Java VM Version: 17.0.2+8
Java  Runtime Version: 17.0.2+8
          OS Platform: Mac OS X

Loc Before: ( 200,  200)
 Loc After: ( 200,  173)

The question is pack() is supposed to change the size of the Frame; why does it change the location? And how to stop it from doing so? Note: this behavior of pack() does not change when I place components in the frame.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文