title: Why is my JPanel not showing on a JFrame?
body: <p>I have made a calculator by referencing to another's source code, but as I try to compile the file, the frame will display, leaving the panel and simply showing nothing but a blank frame. I use a basic <code>frame.add(new Calculator());</code> for the methods within the file. Here is the code:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  
  public class Calculator2 extends JFrame {
      public static void main(String[]args) {
          JFrame frame = new JFrame("Test Calculator");
          Windows c = new Windows();
          c.setVisible(true);
          frame.add(c);
          frame.setSize(500, 500);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  }
  class Windows extends JPanel {
      static boolean[] function = new boolean[4];
      static double[] temporary = {0, 0}; 
      static JTextArea display = new JTextArea(1,20);
  
      public static JPanel Windows() {
          JPanel panel = new JPanel();
          panel.setSize(200, 300);
          GridLayout grid = new GridLayout(5, 5);
          panel.setLayout(grid);
  
          JPanel[] row = new JPanel[5];
          JButton[] buttons = new JButton[19];
          String[] buttonString = {"7","8","9","+",
                                   "4", "5", "6", "-",
                                   "1", "2", "3", "*",
                                   ".", "/", "C", "v",
                                   "+/-", "=", "0"};
          int[] dimW = {300, 45, 100, 90};
          int[] dimH = {35, 40};
          Dimension dispDim = new Dimension(dimW[0], dimH[0]);
          Dimension regDim = new Dimension(dimW[1], dimH[1]);
          Dimension rColDim = new Dimension(dimW[2], dimH[1]);
          Dimension zeroButDim = new Dimension(dimW[3], dimH[1]);
          Font font = new Font("Times new Roman", Font.BOLD, 14);
  
          for(int i = 0; i &lt; 4; i++)
              function[i] = false;
  
          FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
          FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
          for(int x = 0; x &lt; 5; x++)
              row[x] = new JPanel();
          row[0].setLayout(f1);
          for(int x = 0; x &lt; 5; x++)
              row[x].setLayout(f2);
  
          for(int i = 0; i &lt; 19; i++) {
              buttons[i] = new JButton();
              buttons[i].setText(buttonString[i]);
              buttons[i].setFont(font);
              buttons[i].addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent b) {
                      if(b.getSource() == buttons[0])
                          display.append("7");
                      if(b.getSource() == buttons[1])
                          display.append("8");
                      if(b.getSource() == buttons[2])
                          display.append("9");
                      if(b.getSource() == buttons[3]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[0] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[4])
                          display.append("4");
                      if(b.getSource() == buttons[5])
                          display.append("5");
                      if(b.getSource() == buttons[6])
                          display.append("6");
                      if(b.getSource() == buttons[7]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[1] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[8])
                          display.append("1");
                      if(b.getSource() == buttons[9])
                          display.append("2");
                      if(b.getSource() == buttons[10])
                          display.append("3");
                      if(b.getSource() == buttons[11]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[2] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[12])
                          display.append(".");
                      if(b.getSource() == buttons[13]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[3] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[14])
                          clear();
                      if(b.getSource() == buttons[15])
                          getSqrt();
                      if(b.getSource() == buttons[16])
                          getResult();
                      if(b.getSource() == buttons[17])
                          display.append("0");
                  }
              });
          }
  
          display.setFont(font);          
          display.setEditable(false);
          display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
          display.setPreferredSize(dispDim);
          for(int i = 0; i &lt; 14; i++)
              buttons[i].setPreferredSize(regDim);
          for(int i = 14; i &lt; 18; i++) 
              buttons[i].setPreferredSize(rColDim);
          buttons[18].setPreferredSize(zeroButDim);
  
          row[0].add(display);
          panel.add(row[0]);
  
          for(int i = 0; i &lt; 4; i++)
              row[1].add(buttons[i]);
          row[1].add(buttons[14]);
          panel.add(row[1]);
  
          for(int i = 4; i &lt; 8; i++)
              row[2].add(buttons[i]);
          row[2].add(buttons[15]);
          panel.add(row[2]);
  
          for(int i = 8; i &lt; 12; i++)
              row[3].add(buttons[i]);
          row[3].add(buttons[16]);
          panel.add(row[3]);
  
          row[4].add(buttons[18]);
          for(int i = 12; i &lt; 14; i++)
              row[4].add(buttons[i]);
          row[4].add(buttons[17]);
          panel.add(row[4]);
  
          panel.setVisible(true);
  
          return panel;
      }
  
      public static void clear() {
          try {
              display.setText("");
              for(int i = 0; i &lt; 4; i++)
                  function[i] = false;
              for(int i = 0; i &lt; 2; i++)
                  temporary[i] = 0;
          } catch(NullPointerException e) {
          }
      }
      public static void getSqrt() {
          try {
              double value = Math.sqrt(Double.parseDouble(display.getText()));
              display.setText(Double.toString(value));
          } catch(NumberFormatException e) {
          }
      }
      public static void getPosNeg() {
          try {
              double value = Double.parseDouble(display.getText());
              if(value != 0) {
                  value = value * (-1);
                  display.setText(Double.toString(value));
              }
              else {
              }
          } catch(NumberFormatException e) {
          }
      }
      public static void getResult() {
          double result = 0;
          temporary[1] = Double.parseDouble(display.getText());
          String temp0 = Double.toString(temporary[0]);
          String temp1 = Double.toString(temporary[1]);
          try {
              if(temp0.contains("-")) {
                  String[] temp00 = temp0.split("-", 2);
                  temporary[0] = (Double.parseDouble(temp00[1]) *-1);
              }
              if(temp1.contains("-")) {
                  String[] temp11 = temp1.split("-", 2);
                  temporary[1] = (Double.parseDouble(temp11[1]) * -1);
              }
          } catch(ArrayIndexOutOfBoundsException e) {
          }
          try {
              if(function[2] == true)
                  result = temporary[0] * temporary[1];
              else if(function[3] == true)
                  result = temporary[0] / temporary[1];
              else if(function[0] == true)
                  result = temporary[0] + temporary[1];
              else if(function[1] == true)
                  result = temporary[0] - temporary[1];
              display.setText(Double.toString(result));
              for(int i = 0; i &lt; 4; i++)
                  function[i] = false;
          } catch(NumberFormatException e) {
          }
      }
      public final void setDesign() {
          try {
              UIManager.setLookAndFeel(
                      "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch(Exception e) {
          }
      }
      public static void main(String[] args) {
          JPanel pane = new JPanel();
          pane.add(Windows());
          pane.setSize(500, 500);
          pane.setVisible(true);
      }
  }
  </code></pre>
  
  <p>FIXED:
     Had to change the <code>Windows c = new Windows();</code> to <code>JPanel c = Windows.Windows();</code> as it's a method returning a JPanel, rather than a constructor. Credit to: @EdwinTorres &amp; @DirkyJerky</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  
  public class Calculator2 extends JFrame {
      public static void main(String[]args) {
          JFrame frame = new JFrame("Test Calculator");
          Windows c = new Windows();
          c.setVisible(true);
          frame.add(c);
          frame.setSize(500, 500);
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  }
  class Windows extends JPanel {
      static boolean[] function = new boolean[4];
      static double[] temporary = {0, 0}; 
      static JTextArea display = new JTextArea(1,20);
  
      public static JPanel Windows() {
          JPanel panel = new JPanel();
          panel.setSize(200, 300);
          GridLayout grid = new GridLayout(5, 5);
          panel.setLayout(grid);
  
          JPanel[] row = new JPanel[5];
          JButton[] buttons = new JButton[19];
          String[] buttonString = {"7","8","9","+",
                                   "4", "5", "6", "-",
                                   "1", "2", "3", "*",
                                   ".", "/", "C", "v",
                                   "+/-", "=", "0"};
          int[] dimW = {300, 45, 100, 90};
          int[] dimH = {35, 40};
          Dimension dispDim = new Dimension(dimW[0], dimH[0]);
          Dimension regDim = new Dimension(dimW[1], dimH[1]);
          Dimension rColDim = new Dimension(dimW[2], dimH[1]);
          Dimension zeroButDim = new Dimension(dimW[3], dimH[1]);
          Font font = new Font("Times new Roman", Font.BOLD, 14);
  
          for(int i = 0; i &lt; 4; i++)
              function[i] = false;
  
          FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
          FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
          for(int x = 0; x &lt; 5; x++)
              row[x] = new JPanel();
          row[0].setLayout(f1);
          for(int x = 0; x &lt; 5; x++)
              row[x].setLayout(f2);
  
          for(int i = 0; i &lt; 19; i++) {
              buttons[i] = new JButton();
              buttons[i].setText(buttonString[i]);
              buttons[i].setFont(font);
              buttons[i].addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent b) {
                      if(b.getSource() == buttons[0])
                          display.append("7");
                      if(b.getSource() == buttons[1])
                          display.append("8");
                      if(b.getSource() == buttons[2])
                          display.append("9");
                      if(b.getSource() == buttons[3]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[0] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[4])
                          display.append("4");
                      if(b.getSource() == buttons[5])
                          display.append("5");
                      if(b.getSource() == buttons[6])
                          display.append("6");
                      if(b.getSource() == buttons[7]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[1] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[8])
                          display.append("1");
                      if(b.getSource() == buttons[9])
                          display.append("2");
                      if(b.getSource() == buttons[10])
                          display.append("3");
                      if(b.getSource() == buttons[11]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[2] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[12])
                          display.append(".");
                      if(b.getSource() == buttons[13]) {
                          temporary[0] = Double.parseDouble(display.getText());
                          function[3] = true;
                          display.setText("");
                      }
                      if(b.getSource() == buttons[14])
                          clear();
                      if(b.getSource() == buttons[15])
                          getSqrt();
                      if(b.getSource() == buttons[16])
                          getResult();
                      if(b.getSource() == buttons[17])
                          display.append("0");
                  }
              });
          }
  
          display.setFont(font);          
          display.setEditable(false);
          display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
          display.setPreferredSize(dispDim);
          for(int i = 0; i &lt; 14; i++)
              buttons[i].setPreferredSize(regDim);
          for(int i = 14; i &lt; 18; i++) 
              buttons[i].setPreferredSize(rColDim);
          buttons[18].setPreferredSize(zeroButDim);
  
          row[0].add(display);
          panel.add(row[0]);
  
          for(int i = 0; i &lt; 4; i++)
              row[1].add(buttons[i]);
          row[1].add(buttons[14]);
          panel.add(row[1]);
  
          for(int i = 4; i &lt; 8; i++)
              row[2].add(buttons[i]);
          row[2].add(buttons[15]);
          panel.add(row[2]);
  
          for(int i = 8; i &lt; 12; i++)
              row[3].add(buttons[i]);
          row[3].add(buttons[16]);
          panel.add(row[3]);
  
          row[4].add(buttons[18]);
          for(int i = 12; i &lt; 14; i++)
              row[4].add(buttons[i]);
          row[4].add(buttons[17]);
          panel.add(row[4]);
  
          panel.setVisible(true);
  
          return panel;
      }
  
      public static void clear() {
          try {
              display.setText("");
              for(int i = 0; i &lt; 4; i++)
                  function[i] = false;
              for(int i = 0; i &lt; 2; i++)
                  temporary[i] = 0;
          } catch(NullPointerException e) {
          }
      }
      public static void getSqrt() {
          try {
              double value = Math.sqrt(Double.parseDouble(display.getText()));
              display.setText(Double.toString(value));
          } catch(NumberFormatException e) {
          }
      }
      public static void getPosNeg() {
          try {
              double value = Double.parseDouble(display.getText());
              if(value != 0) {
                  value = value * (-1);
                  display.setText(Double.toString(value));
              }
              else {
              }
          } catch(NumberFormatException e) {
          }
      }
      public static void getResult() {
          double result = 0;
          temporary[1] = Double.parseDouble(display.getText());
          String temp0 = Double.toString(temporary[0]);
          String temp1 = Double.toString(temporary[1]);
          try {
              if(temp0.contains("-")) {
                  String[] temp00 = temp0.split("-", 2);
                  temporary[0] = (Double.parseDouble(temp00[1]) *-1);
              }
              if(temp1.contains("-")) {
                  String[] temp11 = temp1.split("-", 2);
                  temporary[1] = (Double.parseDouble(temp11[1]) * -1);
              }
          } catch(ArrayIndexOutOfBoundsException e) {
          }
          try {
              if(function[2] == true)
                  result = temporary[0] * temporary[1];
              else if(function[3] == true)
                  result = temporary[0] / temporary[1];
              else if(function[0] == true)
                  result = temporary[0] + temporary[1];
              else if(function[1] == true)
                  result = temporary[0] - temporary[1];
              display.setText(Double.toString(result));
              for(int i = 0; i &lt; 4; i++)
                  function[i] = false;
          } catch(NumberFormatException e) {
          }
      }
      public final void setDesign() {
          try {
              UIManager.setLookAndFeel(
                      "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch(Exception e) {
          }
      }
      public static void main(String[] args) {
          JPanel pane = new JPanel();
          pane.add(Windows());
          pane.setSize(500, 500);
          pane.setVisible(true);
      }
  }
  </code></pre>