Question: Hi I am trying to set the size of JPanel in JFrame but I am unable to achieve this. How can I achieve my desired output?

Here is my code:

```
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*;
public class Sample extends JFrame 
{ 
private JPanel panel;
private JLabel label1;
public Sample() 
{ 
    panel = new JPanel(); 
    panel.setBackground(Color.YELLOW); 
    panel.setBounds(100, 100, 100, 100);
    ImageIcon icon1 = new ImageIcon("E:\\image\\ZooDelhi\\1.jpg"); 
    label1 = new JLabel(icon1); 
    label1.setLocation(0,0); 
    panel.add(label1);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().add(panel); 
    this.setSize(500,500); 
    this.setVisible(true); 
  setLayout(null);
} 

public static void main (String[] args) {
    new Sample(); 
} 
} 

```

Thanks in advance.

Answer: 
**i will advise not to re-size your panel** 

If you just use the layout managers correctly, they will take care of panel sizes. To avoid having your components stretched out all over the screen when the user increases the window size, have an outermost panel with a left-aligned FlowLayout and the rest of the UI as its single child - that will give the UI its preferred size and any surplus is filled with the background color.
Check reference to understand more.

[Reference](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html)
Per the Oracle documentation:

<http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html>

1. Set the container's layout manager to null by calling
setLayout(null).
2. Call the Component class's setbounds method for
each of the container's children.
3. Call the Component class's
repaint method.

SUGGESTION: try calling "setLayout(null)" *first*.

Also try looking at the sample code in the link above.

'Hope that helps!