Java:JFrame. Setlocationrelativeto (null) is not available as Ubuntu 10.04 / Gnome 2.30 2 window centered openjdk 1.6 0_ eighteen
•
Java
Example 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 http://img193.imageshack.us/img193/5296/screenshotrev.png
Is this openjdk's fault? I remember hearing that it is not as good as sun's, but because it has become the standard of Ubuntu, or the standard I decided to use with it The program may run on windows, so I think I want to check there... Any simple way to solve this platform independent way without destroying where it already works?
Solution
One way is to position the window manually Place the following code after calling 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 is only tested on windows
In addition, you should always use setpreferredsize() instead of setsize()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码