Trying to create the simplest application. But, when adding a JPanel component to a JFrame , JPanel does not occupy all the allowable space. The program produces the following result: "java.awt.Dimension[width=0,height=0]" , i.e. the JPanel dimensions are zero. How to make Jpanel placed in a JFrame automatically occupy all the space available in JFrame in Java ? And one could immediately find out these sizes?

Program Listing:

 public class Test1 extends JFrame{ Test1() { super("ГрафичСскоС ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅"); setBounds(50,50,1000, 450); JPanel panel = new JPanel(); this.add(panel); System.out.println(this.getComponent(0).getSize()); } public static void main(String[] args) throws IOException { new Test1(); } } 
  • one
    it will stretch itself when the frame becomes visible ( BorderLayout used by default) - zRrr

1 answer 1

You were prompted correctly in the comments (user zRrr ), try to "run" the following code:

 public class Test1 extends JFrame{ Test1 () { super("ГрафичСскоС ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅"); setBounds(50,50,1000, 450); JPanel panel = new JPanel(); panel.setBackground(Color.BLUE); this.setContentPane(panel); } public static void main(String[] args){ Test1 test = new Test1(); test.setVisible(true); System.out.println(test.getContentPane().getSize()); } }