title: Change JFrame's background color
body: <p>I am following a video tutorial which tries to make a Java game in 2d. I have found that course's author's approach is not working properly because of changing JFrame's color doesn't show up!</p>
  
  <p>His approach, the <strong><em>Window</em></strong> class:</p>
  
  <pre><code>package Modelos;
  import java.awt.Color;
  import java.awt.event.*;
  import javax.swing.JFrame;
  
  public class Ventana extends JFrame implements WindowListener  {
  
      public Ventana(String titulo){
          super(titulo);
          this.setSize(800,600);
          addWindowListener(this);
          setBackground(Color.BLACK);
      }
  
      @Override
      public void windowOpened(WindowEvent we) {
      }
  
      @Override
      public void windowClosing(WindowEvent we) {
          System.exit(0);
      }
  
      @Override
      public void windowClosed(WindowEvent we) {
      }
  
      @Override
      public void windowIconified(WindowEvent we) {
      }
  
      @Override
      public void windowDeiconified(WindowEvent we) {
      }
  
      @Override
      public void windowActivated(WindowEvent we) {
      }
  
      @Override
      public void windowDeactivated(WindowEvent we) {
      }
  }
  </code></pre>
  
  <p><strong><em>Panel</em></strong> class which is supposed to graph rectangles, triangles, circles to represent game's objects:</p>
  
  <pre><code>package Modelos;
  import java.awt.*;
  import java.util.ArrayList;
  import javax.swing.JPanel;
  
  public class PanelFG extends JPanel {
  
      ArrayList aDibujar;
  
      public PanelFG(ArrayList Dibujar){
          this.aDibujar=Dibujar;
      }
  
      public void print(Graphics g){
          Dibujable dib;
          for (int i = 0; i &lt;aDibujar.size(); i++) {
              dib = (Dibujable) aDibujar.get(i);
              dib.dibujar(g);
          }
      }
  }
  </code></pre>
  
  <p>And <strong>Main</strong></p>
  
  <pre><code>package Ejecuciones;
  import Modelos.*;
  import java.awt.Button;
  import java.awt.Color;
  import java.util.ArrayList;
  
  public class Main {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          Ventana nuestraVentana = new Ventana("Juego de Naves");
          ArrayList ArregloDeObjetos = new ArrayList();
  
          PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);
  
          nuestraVentana.add(nuestroPanel);
  
          nuestraVentana.setSize(800,600);
          nuestraVentana.setVisible(true);
      }
  }
  </code></pre>
  
  <p>RESULT:
  <a href="https://i.stack.imgur.com/QkYhe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QkYhe.png" alt="White JFRAME"></a></p>
  
  <p>However my approach:</p>
  
  <p>WINDOW:</p>
  
  <pre><code>package Modelos;
  import java.awt.Color;
  import java.awt.event.*;
  import javax.swing.JFrame;
  
  public class Ventana extends JFrame implements WindowListener  {
  
      public Ventana(String titulo){
          super(titulo);
          this.setSize(800,600);
          addWindowListener(this);
          this.getContentPane().setBackground(Color.BLACK);
          this.setVisible(true);
      }
  
      @Override
      public void windowOpened(WindowEvent we) {
      }
  
      @Override
      public void windowClosing(WindowEvent we) {
          System.exit(0);
      }
  
      @Override
      public void windowClosed(WindowEvent we) {
      }
  
      @Override
      public void windowIconified(WindowEvent we) {
      }
  
      @Override
      public void windowDeiconified(WindowEvent we) {
      }
  
      @Override
      public void windowActivated(WindowEvent we) {
      }
  
      @Override
      public void windowDeactivated(WindowEvent we) {
      }
  }
  </code></pre>
  
  <p>PANEL class is equal.</p>
  
  <p>MAIN:</p>
  
  <pre><code>package Ejecuciones;
  import Modelos.*;
  import java.awt.Button;
  import java.awt.Color;
  import java.util.ArrayList;
  
  public class Main {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          Ventana nuestraVentana = new Ventana("Juego de Naves");
          ArrayList ArregloDeObjetos = new ArrayList();
  
          PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LQt9s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LQt9s.png" alt="BLACK correct window"></a>
  I think it is due to the author did not used getContentPane() when creating the main JFrame to set its background to black, and I did.</p>
  
  <p>I have followed the topic: <a href="https://stackoverflow.com/questions/2742270/jframe-setbackground-not-working-why">JFrame.setBackground() not working -- why?</a></p>
  
  <p>Also I do not understand how a code would work properly and other do not, considering that we do both are using Netbeans 8.1.</p>
  
  <p>Any opinion/suggestion/explanation?</p>