简体   繁体   中英

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? 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?

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

One way is to manually position the window. Put the following code right after your call to 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.

Also, you should always use setPreferredSize() instead of 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). 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. 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(). Frame's getWidth() and getHeight() return different (correct) values after packing and that's why OP is unable to center.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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