为什么 JFrame pack() 会影响位置?
我查看了 不可调整大小的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论