title: Why calling JFrame.pack() after JFrame.setVisible(true) affect the location where JFrame is displayed?
body: <p>I am compiling and running the following code on Debian 8 with XFCE 4 desktop.</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JComponent;
  import java.awt.EventQueue;
  import java.awt.Dimension;
  import java.awt.Graphics;
  
  public class FrameDemo
  {
      public static void main(String[] args)
      {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  JFrame frame = new JFrame();
                  frame.setLocationByPlatform(true);
                  frame.add(new HelloWorldComponent());
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.pack();
                  frame.setVisible(true);
              }
          });
      }
  }
  
  class HelloWorldComponent extends JComponent
  {
      public void paintComponent(Graphics g)
      {
          g.drawString("hello, world", 50, 50);
      }
  
      public Dimension getPreferredSize()
      {
          return new Dimension(200, 100);
      }
  }
  </code></pre>
  
  <p>This produces the desired output. The frame is centered on the desktop.</p>
  
  <p><a href="https://i.stack.imgur.com/QxXXg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QxXXg.png" alt="enter image description here"></a></p>
  
  <p>But when I move the <code>frame.pack()</code> statement after <code>frame.setVisible(true)</code> statement, i.e. I pack after I show the frame, I don't see the desired output anymore.</p>
  
  <pre><code>                JFrame frame = new JFrame();
                  frame.setLocationByPlatform(true);
                  frame.add(new HelloWorldComponent());
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
                  frame.pack();
  </code></pre>
  
  <p>The frame is now displayed at the top-left corner of the desktop.</p>
  
  <p><a href="https://i.stack.imgur.com/7KdD6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7KdD6.png" alt="enter image description here"></a></p>
  
  <p>In fact, the frame does appear in the center of the desktop very briefly for a fraction of a second. After a split second or so, the frame moves to the top-left corner of the desktop.</p>
  
  <p>Why moving the location of the <code>frame.pack()</code> statement change the location where the frame appears on the desktop?</p>