In another thread I stated that I liked to center my GUIs by doing something like this:
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new HexagonGrid());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
But Andrew Thompson had a different opinion, to instead call
frame.pack();
frame.setLocationByPlatform(true);
and inquiring minds want to know why?
To my eye, a GUI in the middle of the screen looks so.. “splash-screen’ish”. I keep waiting for them to disappear and the real GUI to appear!
Since Java 1.5 we’ve had access to
Window.setLocationByPlatform(boolean). which..Have a look at the effect of this example that puts 3 GUIs into the default positions as chosen by the OS – on Windows 7, Linux with Gnome & Mac OS X.
(3 lots of) 3 GUIs neatly stacked. This represents ‘the path of least surprise’ for the end user, since it is how the OS might position 3 instances of the default plain-text editor (or anything else, for that matter). My thanks to trashgod for the Linux & Mac. images.
Here is the simple code used: