title: Why is it that my JButtons do not show up when put in the constructor for the JPanel?
body: <p>I am new to GUI in Java (and coding in gneral I guess), so I am confused on why the JButtons on my JPanel only show up if I initialize and add the JButtons on the class that has the JFrame.  What I am trying to do is create a phone keypad with JBUttons and a GridLayout on a JPanel.</p>
  
  <p>If I initialize and add the JButtons to the JPanel on the class that has the JFrame in it.  I can not add the JButtons to the JPanel in the class that extends the JPanel and then add the JPanel object to the JFrame.</p>
  
  <p>This works:</p>
  
  <pre><code>public static void main (String[] argv){ 
           JFrame frame = new JFrame();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  
           frame.setBackground(Color.gray);
           DialPanel keypad = new DialPanel();
           panel.setLayout(new GridLayout(3, 3));
           JButton one = new JButton("1");
       JButton two = new JButton("2");
       JButton three = new JButton("3");
       JButton four = new JButton("4");
       JButton five = new JButton("5");
       JButton six = new JButton("6");
       JButton seven = new JButton("7");
       JButton eight = new JButton("8");
       JButton nine = new JButton("9");
       keypad.add(one);
       keypad.add(two);
       keypad.add(three);
       keypad.add(four);
       keypad.add(five);
       keypad.add(six);
       keypad.add(seven);
       keypad.add(eight);
       keypad.add(nine);
  
           frame.getContentPane().add(keypad);
           keypad.setVisible(true);
  
           frame.pack();
           frame.setVisible(true);
      } 
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class DialPanel extends JPanel {
  
      DialPanel(){
          JPanel panel = new JPanel();
      }
  }
  </code></pre>
  
  <p>But this does not work, and I am not sure why:</p>
  
  <pre><code>    public static void main (String[] argv){ 
           JFrame frame = new JFrame();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  
           frame.setBackground(Color.gray);
           DialPanel keypad = new DialPanel();
           frame.getContentPane().add(keypad);
           keypad.setVisible(true);
  
           frame.pack();
           frame.setVisible(true);
      } 
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class DialPanel extends JPanel {
  
      DialPanel(){
           JPanel panel = new JPanel();
           panel.setLayout(new GridLayout(3, 3));
           panel.setBackground(Color.gray);
                   JButton one = new JButton("1");
           JButton two = new JButton("2");
           JButton three = new JButton("3");
           JButton four = new JButton("4");
           JButton five = new JButton("5");
           JButton six = new JButton("6");
           JButton seven = new JButton("7");
           JButton eight = new JButton("8");
           JButton nine = new JButton("9");
           panel.add(one);
           panel.add(two);
           panel.add(three);
           panel.add(four);
           panel.add(five);
           panel.add(six);
           panel.add(seven);
           panel.add(eight);
           panel.add(nine);
      }
  }
  </code></pre>
  
  <p>With the second option the window is simply just blank, but with everything in the main method it works as intended. Why won't having the buttons in the constructor work, and how can I get it to work (if I can get it to work like that at all)?</p>