Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <em>real</em> GUI to appear!</p> <p>Since Java 1.5 we've had access to <a href="http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#setLocationByPlatform-boolean-" rel="noreferrer"><code>Window.setLocationByPlatform(boolean)</code></a>. which..</p> <blockquote> <p>Sets whether this Window should appear at the default location for the native windowing system or at the current location (returned by getLocation) the next time the Window is made visible. This behavior resembles a native window shown without programmatically setting its location. <strong>Most windowing systems cascade windows if their locations are not explicitly set.</strong> The actual location is determined once the window is shown on the screen. </p> </blockquote> <p>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 &amp; Mac OS X.</p> <p><img src="https://i.stack.imgur.com/D1JLb.png" alt="Stacked windows on Windows 7"> <img src="https://i.stack.imgur.com/5kvJV.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/J2zQu.png" alt="Stacked windows on Mac OS X"></p> <p>(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 &amp; Mac. images.</p> <p>Here is the simple code used:</p> <pre><code>import javax.swing.*; class WhereToPutTheGui { public static void initGui() { for (int ii=1; ii&lt;4; ii++) { JFrame f = new JFrame("Frame " + ii); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); String s = "os.name: " + System.getProperty("os.name") + "\nos.version: " + System.getProperty("os.version"); f.add(new JTextArea(s,3,28)); // suggest a size f.pack(); // Let the OS handle the positioning! f.setLocationByPlatform(true); f.setVisible(true); } } public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception useDefault) {} initGui(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload