0

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.

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • never use `null` layout. – Braj Jul 23 '14 at 07:18
  • 2
    Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are **not** conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Jul 23 '14 at 07:18
  • 1
    what is your desired output? share it. – Braj Jul 23 '14 at 07:18
  • @Braj i want to set jpanel on my specific position – user3852674 Jul 23 '14 at 07:19
  • what about if system resolution is changed or OS is changed. – Braj Jul 23 '14 at 07:21
  • i have to use this application on single pc – user3852674 Jul 23 '14 at 07:25
  • 2
    Pixel perfect layouts are an illusion in modern UI design, you don't control the properties that change the space components need, such as DPI, font metrics, resolution and other rendering pipeline constraints. While you "might think" you don't need a layout manager, you will spend more time trying to solve problems without them, as Swing has been designed at it's core to utilise them. You will also have issues with the frame borders, which are variable across platforms which will make it difficult to get the window the right size – MadProgrammer Jul 23 '14 at 07:33
  • Provide ASCII art or a simple drawing of how the GUI should appear at default size and (if resizable) with extra width/height. – Andrew Thompson Jul 23 '14 at 07:50

3 Answers3

1

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

mlethys
  • 436
  • 9
  • 29
1

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

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
-1

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!

FoggyDay
  • 11,962
  • 4
  • 34
  • 48