Adding JDesktopPane to JFrame using GridBagLayout

2019-09-20 05:35发布

问题:

How do I add the JDesktopPane to JFrame using GridBagLayout and set its height and width. If I add JDesktopPane that contains JInternalFrame I don't get anything. But works well in case of GridLayout but the problem is I can't set my desired size in it as GridLayout splits equal space among each component added.

回答1:

You will probably need to set the fill and weight attributes of the GridBagConstraints...

GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;

This will cause the component to want to push to the limits of the container and will cause the component to fill it's cell within the grid

This override the components preferred size (for the most part)

Take a look at How to Use GridBagLayout for more details...