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?
解决方案
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().