Why is the JFrame larger than its ContentPane, even though it is a certain size?

I noticed strange behavior between containers in the swing.

To illustrate the test, I created a JFrame and JPanel and set the panel to be contentPane. I have defined the preferred and maximum JPanel size to 400 300. All this can be seen in the following example:

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

      

Result:

inserir a descriçã da da imagem aqui

To my surprise, the terminal output is:

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

I didn't understand why the frame added this extra space without even having anything in the component that makes the frame resize.

I tried setting the zeroed edges on the panel by adding the line panel.setBorder (BorderFactory.createEmptyBorder(0, 0, 0, 0));

before pack();

, but the result is still the same.

The problem is that java is giving me false information as the panel is scaled to the frame size and from the above result we saw that the two are not the same size.

Are the two measures listed correctly? Why is this happening?

+2


source to share


1 answer


You may find it helpful to compare the result with a similar program on another platform. In particular,

  • Red Border

    is inside the panel; your usage is in accordance with the API recommendation , "we recommend putting the component in JPanel

    and setting the border to JPanel

    ."

  • The frame width matches the pane width on Mac OS X because the super-heavy peer used for the top layer does not have a vertical border on Mac OS X; Windows peer-to-peer network has its own boundary.

  • Frame height includes drag bar and border: 38 pixels on Widows, 22 pixels on Mac OS X.

  • Don't use setPreferredSize()

    when you really want to override getPreferredSize()

    .

  • As an aside, the frame method add()

    navigates to the content area.

panel size: [400,300]
frame size: [400,322]

      



image

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() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        };
        panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

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

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

      

+2


source







All Articles