Java:JFrame.setLocationRelativeTo(null) 未在 Ubuntu 10.04/gnome 2.30.2 和 OpenJDK 1.6.0_18 上居中窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3480102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:09:23  来源:igfitidea点击:

Java: JFrame.setLocationRelativeTo(null) not centering the window on Ubuntu 10.04 / gnome 2.30.2 with OpenJDK 1.6.0_18

javaubuntujframegnome

提问by captain poop

Sample code:

示例代码:

    JFrame jFrame = new JFrame("Test");
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setLocationRelativeTo(null);
    jFrame.setSize(600, 600);
    jFrame.pack();
    // jFrame.setLocationRelativeTo(null); // same results
    jFrame.setVisible(true);

screenshot

截屏

Is this the OpenJDK's fault? I recall hearing it wasn't as good as Sun's, but since it became the standard for Ubuntu or whatever I decided to go along with it. The program is probably gonna run on windows, so I suppose I'm gonna have to check there... Any easy way to fix this in a platform independent way without breaking it where it already works?

这是 OpenJDK 的错吗?我记得听说它不如 Sun 的好,但是因为它成为了 Ubuntu 或任何我决定采用它的标准。该程序可能会在 Windows 上运行,所以我想我必须在那里检查......有什么简单的方法可以以独立于平台的方式解决这个问题而不破坏它已经可以工作的地方?

采纳答案by jjnguy

One way is to manually position the window. Put the following code right after your call to pack().

一种方法是手动定位窗口。在您调用pack().

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point middle = new Point(screenSize.width / 2, screenSize.height / 2);
Point newLocation = new Point(middle.x - (jFrame.getWidth() / 2), 
                              middle.y - (jFrame.getHeight() / 2));
jFrame.setLocation(newLocation);

Disclaimer, this was only tested on windows.

免责声明,这仅在 Windows 上进行了测试。

Also, you should always use setPreferredSize()instead of setSize().

此外,您应该始终使用setPreferredSize()而不是setSize().

回答by Evan

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setVisible(true);
jFrame.setLocationRelativeTo(null); //To center the code

This will correct the problem and center the Jframe

这将纠正问题并使 Jframe 居中

回答by Nicholas Duchon

jFrame.validate();

This actually works better since pack can change the frame size, while validateleaves the frame size alone.

这实际上效果更好,因为 pack 可以更改帧大小,而validate单独保留帧大小。

回答by mrazjava

I know this is an old question, but setLocationRelativeTo() will work but it must be called after pack(). Frame's getWidth() and getHeight() return different (correct) values after packing and that's why OP is unable to center.

我知道这是一个老问题,但 setLocationRelativeTo() 会起作用,但必须在 pack() 之后调用它。Frame 的 getWidth() 和 getHeight() 在打包后返回不同(正确)的值,这就是 OP 无法居中的原因。

回答by YoannFleuryDev

Just a precision : If you set the location before the size of the frame, you will center the top left corner of the window because the size is (0,0). You have to set the size before the location.

只是一个精度:如果你在框架的大小之前设置位置,你将在窗口的左上角居中,因为大小是 (0,0)。您必须在位置之前设置大小。

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);

It works well with me with OpenJDK-6 and Ubuntu 13.04. Try it on other platforms.

它适用于 OpenJDK-6 和 Ubuntu 13.04。在其他平台上试试。

回答by LahiruBandara

You should not declare the jFrame size before giving the relative location. If you do that what happens is that will draw your iFrame away from the given location.

在给出相对位置之前,您不应声明 jFrame 大小。如果你这样做会发生什么是将你的 iFrame 从给定的位置拉开。

This is wrong----

这是错误的----

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);
jFrame.pack();
jFrame.setVisible(true);

This is right----

这是对的 - -

JFrame jFrame = new JFrame("Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//size comes first
jFrame.setSize(600, 600);

//and then the position
jFrame.setLocationRelativeTo(null);

jFrame.pack();
jFrame.setVisible(true);

回答by trinity420

Just set the size beforesetting the location.

只需设置位置之前设置大小。

Wrong:

错误的:

jFrame.setLocationRelativeTo(null);
jFrame.setSize(600, 600);

Correct:

正确的:

jFrame.setSize(600, 600);
jFrame.setLocationRelativeTo(null);

Note: Call setVisible()at last to prevent "jumping" of the window.

注意:最后调用setVisible()以防止窗口“跳跃”。