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);
    }
}

```
JFrame by default use BorderLayout, so when you not specify any layout it work because your panel will be added to the center of the BorderLayout.

but when you specify FlowLayout which I'm not adviced you to do because it will arrange all components on row and then exapend to multiple rows when needed. and the other thing FlowLayout cann't get your preferredSize, and you need to provide this size on your custom panel.

> 
> A flow layout lets each component assume its natural (preferred) size.
> 
> 
> 

<http://docs.oracle.com/javase/7/docs/api/java/awt/FlowLayout.html>

so either you specify your panel size, for example in your paintComponent of the panel:

```
setSize(200, 100); // values depend on your components

```

or use any other layout like the default one (BorderLayout) or other types like BoxLayout, it depend on what you want the frame to be look like

see this for more layouts types:
<http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html>