简体   繁体   English

Java:JFrame.setLocationRelativeTo(null)不使用OpenJDK 1.6.0_18将窗口置于Ubuntu 10.04 / gnome 2.30.2的中心

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

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);

截图

Is this the OpenJDK's fault? 这是OpenJDK的错吗? 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. 我记得听说它不如Sun的那么好,但是因为它成了Ubuntu的标准,或者我决定与它一起使用的任何东西。 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? 该程序可能会在Windows上运行,所以我想我将不得不在那里检查...任何简单的方法以平台无关的方式解决这个问题而不会破坏它已经运行的地方?

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居中

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()

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). 只是一个精度:如果您在框架大小之前设置位置,您将在窗口的左上角居中,因为大小为(0,0)。 You have to set the size before the location. 您必须在该位置之前设置大小。

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. OpenJDK-6和Ubuntu 13.04适用于我。 Try it on other platforms. 在其他平台上试用。

jFrame.validate();

这实际上更好,因为pack可以改变帧大小,而validate留下帧大小。

I know this is an old question, but setLocationRelativeTo() will work but it must be called after pack(). 我知道这是一个老问题,但setLocationRelativeTo()可以工作但必须在pack()之后调用。 Frame's getWidth() and getHeight() return different (correct) values after packing and that's why OP is unable to center. Frame的getWidth()和getHeight()在打包后返回不同(正确)的值,这就是OP无法居中的原因。

You should not declare the jFrame size before giving the relative location. 在给出相对位置之前,不应声明jFrame大小。 If you do that what happens is that will draw your iFrame away from the given location. 如果您这样做会发生什么,这将使您的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);

Just set the size before setting 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()以防止窗口“跳跃”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM