Question: I don;t understand how we can use same JLabel variable to create a new label. Doesn't aLabel variable here refers to the same single object(label). So how could we get 4 label ?

Can i do this behavior with other classes objects to or just with component classes?

Explanation of Step by step flow of execution of code here would be helpful.

```
 JLabel label1 =newJLabel("First label"); 
add(label1, BorderLayout.NORTH); 
JLabel label2 =newJLabel("Second label"); 
add(label2, BorderLayout.SOUTH); 
JLabel label3 =newJLabel("Third label"); 
add(label3, BorderLayout.CENTER); 

```

OR i can just use: 

```
JLabel aLabel =newJLabel("First label"); 
        add(aLabel, BorderLayout.NORTH); 
        aLabel =newJLabel("Second label"); 
        add(aLabel, BorderLayout.SOUTH); 
        aLabel =newJLabel("Third label"); 
        add(aLabel, BorderLayout.CENTER);

```

Answer: 
As mentioned in another answer, the custom drawn panel has no inherent size. It should *return* a preferred size suitable for the content. Then we just need to `pack()` the frame after it is added.

![enter image description here](https://i.stack.imgur.com/jVKqC.png)

```
import java.awt.*;
import javax.swing.*;

class GuiForJFrame extends JFrame {
    private FlowLayout layout;
    private Container container;

    public GuiForJFrame() {
        super("Drawing Graphics");

        // Setting the Layout
        layout = new FlowLayout(FlowLayout.LEFT);
        container = getContentPane();
        setLayout(layout);
    }

    public static void main(String[] args) {
        // Creating the JFrame object
        GuiForJFrame jFrame = new GuiForJFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adding Graphics to JFrame
        GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
        jFrame.add(graphics);
        //jFrame.setSize(500, 200);
        jFrame.pack();
        jFrame.setVisible(true);
    }
}

class GuiForDrawingGraphics extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

        g.setColor(Color.RED);
        g.fillRect(25, 25, 150, 50);

        g.setColor(new Color(156, 32, 111));
        g.fillRect(25, 80, 150, 50);

        g.setColor(Color.BLACK);
        g.drawString("Drawing Graphics in JAVA", 25, 150);

    }

    public Dimension getPreferredSize() {
        return new Dimension(300,200);
    }
}

```
Add `jFrame.pack();` and `jFrame.validate();`. This sets all child components (i.e. components inside jFrame) as well as jFrame itself to their appropriate size and makes sure they can even be displayed and that no `Exception`s will be thrown.

If you don't do either of these, nothing will (and nothing should) show up.

So, your code for the `Application` class should look like:

```
public class Application {
    public static void main(String[] args) {
        // Creating the JFrame object
        GuiForJFrame jFrame = new GuiForJFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Adding Graphics to JFrame
        GuiForDrawingGraphics graphics = new GuiForDrawingGraphics();
        jFrame.add(graphics);
        jFrame.setSize(500, 200);

        // Displaying the JFrame properly.
        jFrame.pack();
        jFrame.validate();
        jFrame.setVisible(true);
    }
}

```

In addition to this, you'll have to use the `setPreferredSize(Dimension dimension)` method to specify the size that you want each `JComponent` to be.

Check out the `JComponent` and `JFrame` classes at <http://docs.oracle.com/javase/7/docs/api/> for more information.