Why does the size of the JFrame exceed the size of its ContentPane, even if it has a defined size?

4

I noticed a strange behavior between containers in the swing.

To exemplify the test, I created a JFrame and a JPanel , and set the panel to contentPane of JFrame . Set the preferred and maximum size of the JPanel to 400,300. All this can be seen in the example below:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ExcessiveSpacePanelTest {
    JFrame frame;
    JPanel panel;

    public void initGUI(){

        frame =  new JFrame();

        panel = new JPanel();
        panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
        panel.setPreferredSize(new Dimension(400, 300));
        panel.setMaximumSize(new Dimension(400, 300));


        frame.setContentPane(panel);
        frame.pack();

        System.out.println("panel size: [" + panel.getSize().width + "," + panel.getSize().height +"]");
        System.out.println("frame size: [" + frame.getSize().width + "," + frame.getSize().height+"]");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() ->{
            new ExcessiveSpacePanelTest().initGUI();
        });
    }
}

The result of the screen is:

To my surprise, the terminal output is:

  

panel size: [400,300]
  frame size: [416,338]

I did not understand why the frame added this extra space, even though it had nothing in the component that forces the frame to resize.

I tried to set zeroed edges on the panel by adding the line panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); before the pack() but even then the result is the same.

The problem with this is that java is giving me false information, since the panel is scaling in the size of the frame, and by the above result, we have seen that the two are not the same size.

Are the two measures being reported correctly? Why does this occur?

    
asked by anonymous 10.05.2017 / 20:00

1 answer

4

That's right. This extra space refers to window borders , these borders count in size.

For example, in Windows 10, windows have a 1px border on each side. Otherwise, there is one more invisible 7px border to steady the mouse pointer when changing the size of the window. Adding these borders, we have 16, which is exactly the difference between the usable area of Frame its actual size in my example.

You can capture the size of the JFrame "usable" area using getContentPane() even without pre-defining a component to be ContentPane . That is, there already exists a Panel that occupies the maximum "usable" size of the window.

See an example

public void initGUI(){
    frame =  new JFrame();

    frame.setPreferredSize(new Dimension(400, 300));
    frame.pack();

    System.out.println("Content pane size: [" + frame.getContentPane().getSize().width + ", " + frame.getContentPane().getSize().height + "]");
    System.out.println("frame size: [" + frame.getSize().width + "," + frame.getSize().height+"]");

    frame.setVisible(true);
}

The output is:

  

Content pane size: [384, 261]
  frame size: [400,300]

    
10.05.2017 / 20:40