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 think that it doesn't matter if you have layout or don't. Not having layout wouldn't harm changing size of JPanel.
Try to use this: 

```
myPanel.setPreferredSize(new Dimension(500, 500));

```

I hope it'll help ;)
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!