1

In following code I have set background color the content pane of the JFrame to black and JPanel to red. But when I am trying to setSize() with different sizes, red area does not contract or expend why is it so?

import java.awt.*;
import javax.swing.*;
public class exp{  
    public static void main(String args[]){ 
        JFrame jf=new JFrame("This is JFrame");
        JPanel h=new JPanel();
        h.setSize(400,500);

        h.add(new JButton("Button"));
        h.add(new JLabel("this is JLabel"));
        h.setBackground(Color.RED);

        jf.add(h);
        jf.getContentPane().setBackground(Color.BLACK);
        jf.setLayout(new FlowLayout());


        jf.setVisible(true);
        jf.setSize(600,800);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }  
}
abhijeet
  • 85
  • 2
  • 10

4 Answers4

4

A JPanel is typically used as a container for components or other containers. The size of the panel is determined more by the size of the content and the layout and layout constraint with which it is added, than any size set on it.

What you seem to by trying here, can best be achieved by setting an EmptyBorder to the panel. E.G.

enter image description hereenter image description here

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

public class exp{
    public static void main(String args[]){
        Runnable r = new Runnable() {
            public void run() {
                JFrame jf=new JFrame("This is JFrame");
                JPanel h=new JPanel();
                // add more space around the panel!
                h.setBorder(new EmptyBorder(50,50,50,50));

                h.add(new JButton("Button"));
                h.add(new JLabel("this is JLabel"));
                h.setBackground(Color.RED);

                jf.add(h);
                jf.getContentPane().setBackground(Color.BLACK);
                jf.setLayout(new FlowLayout());

                jf.setVisible(true);
                jf.pack();
                jf.setMinimumSize(jf.getSize());
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Just to meet the requirements of the title, JPanel is a concert implementation of JComponent (which is abstract) and is opaque (where as JComponent is transparent) – MadProgrammer Jan 20 '14 at 09:23
  • 1
    @MadProgrammer *"is a concert implementation"* That harp needs a tune. I think you meant 'concrete'. ;) – Andrew Thompson Jan 20 '14 at 09:28
  • 1
    Most likely, but I had a 20 month old who thought is was fun splashing water onto my socks from her bath at the time, so you're lucky that was the only mistake ;) – MadProgrammer Jan 20 '14 at 10:33
  • @MadProgrammer *"thought is was fun splashing water onto my socks"* ..Whatta'ya'mean by 'thought'? I **still** think that's fun. :) – Andrew Thompson Jan 20 '14 at 10:46
0

jf.setContentPane(h);

i think u missed to set it as contentpane so it is just another panel

XXX
  • 181
  • 7
0

But when I am trying to setSize() with different sizes, red area does not contract or expend why is it so?

Because you are using FlowLayout() for your Jframe. Try to use different layout to do what you expect.Like:

Replace

jf.setLayout(new FlowLayout());

By

jf.setLayout(new BorderLayout());

Now change the size in setSize() you will get output as you expect.

For more help see Layout Managers

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
-1

Option 1: Use setPreferredSize() instead of setSize(). It will run as expected. Panel will take size as mentioned (400 * 400) with red backbng color.

Option 2: If you will comment this line (jf.setLayout(new FlowLayout());) it will paint whole frame red as panel will occupy whole frame size.

Check why this is happening in the following post: Java: Difference between the setPreferredSize() and setSize() methods in components.

Community
  • 1
  • 1
amit
  • 39
  • 1
  • 7