title: Should I add components to a JFrame or to its contentPane?
body: <p>When I learned creating Java GUI:s in my first Java course, I was taught to create my windows as <code>JFrame</code> instances, and then add a <code>JPanel</code> to each <code>JFrame</code> and finally add all the GUI components to the <code>JPanel</code>:</p>
  
  <pre><code>class Example extends JFrame {
      Example() {
          JPanel panel = new JPanel();
          this.add(panel);
  
          // Create components here and add them to panel
          // Perhaps also change the layoutmanager of panel
  
          this.pack();
          this.setVisibility(true);
      }
  
      public static void main(String[] args) {
          new Example();
      }
  }
  </code></pre>
  
  <p>I always though "well, this smells a little; I don't like creating an extra object just to be a container," but I didn't know any other way to do it so I just went on with it. Until recently, when I stumbled over this "pattern":</p>
  
  <pre><code>class AnotherExample extends JFrame {
      AnotherExample() {
          Container pane = this.getContentPane();
  
          // Add components to and change layout of pane instead
  
          this.pack();
          this.setVisibility(true);
      }
  
      public static void main(String[] args) {
          new AnotherExample();
      }
  }
  </code></pre>
  
  <p>Still being quite new to Java, I feel better about the second approach just because it doesn't involve creating a <code>JPanel</code> just to wrap the other components. But what are the real differences between the approaches, except from that? Does any one of them have any great benefits over the other?</p>