title: Create custom UI elements in Java
body: <p>I want to draw customized elements on a JFrame. </p>
  
  <p>I've tried it by creating a class UI (extends JFrame) and a class Component (extends JPanel). The component draws something on itself and the UI just adds this component. So until now, I've written this code: </p>
  
  <p>File UI.java</p>
  
  <pre><code>package UIComponent;
  
  import javax.swing.JFrame; 
  
  public class UI extends JFrame {
  
      public UI(){
          this.setSize(1024,684);
          this.setTitle("This is just a test program."); 
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          this.add(new Component(20,20,20,20)); 
          this.add(new Component(40,30,20,20)); 
      }
  
  }
  </code></pre>
  
  <p>File Component.java</p>
  
  <pre><code>package UIComponent;
  
  import java.awt.Color;
  import javax.swing.JPanel; 
  import java.awt.Graphics; 
  
  public class Component extends JPanel {
  
      int x, y, w, h; 
  
      public Component(int x, int y, int w, int h){
          this.x = x; 
          this.y = y; 
          this.w = w; 
          this.h = h; 
      }
  
      @Override
      public void paintComponent(Graphics g){
          g.setColor(Color.red);
          g.fillRect(this.x, this.y, this.w, this.h);
      }
  
  }
  </code></pre>
  
  <p>But the result is not that what I accept. It draws just one Rectangle. </p>