初始化面板并将其设置为GridBagLayout时,两个封装的面板不会展开以填充所有可用区域。像这样:
代码:
containerPanel.setLayout(new GridBagLayout());
add(containerPanel);
contentPanel = new JPanel();
constraints.fill = GridBagConstraints.VERTICAL;
constraints.gridx = 0;
constraints.gridy = 0;
containerPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
containerPanel.add(contentPanel, constraints);
sidebarPanel = new JPanel();
constraints.fill = GridBagConstraints.VERTICAL;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx = 0.33;
constraints.weighty = 2;
sidebarPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
containerPanel.add(sidebarPanel, constraints);