Extra white space when using BoxLayout

2019-08-13 17:29发布

I'm using Java Swing to make a simple GUI using a BoxLayout LayoutManager. It is basically composed of a single JPanel, with additional JPanels able to be added by the user.

With two JPanels (the first one, and one additional), the format is proper:

http://i.stack.imgur.com/lg9Wd.png

But when even more JPanels are added, extra space is added to the left side of the frame:

http://i.stack.imgur.com/YSnFA.png

Here's what I have:

public void updateListFrameContentPane(Container mainPane) {
    mainPane.setLayout(new GridLayout(1,1+chatPanels.size()));
    JPanel firstPanel = new JPanel();
    firstPanel.setLayout(new BoxLayout(firstPanel, BoxLayout.Y_AXIS));
    firstPanel.add(friendsLabel);        
    firstPanel.add(listScrollPane);
    mainPane.add(firstPanel);
    for(JPanel chatPanel : chatPanels)
       mainPane.add(chatPanel);
}

Where chatPanels is a HashSet<JPanel>. I tried using the setMaximumSize method on each JPanel to allow them to grow larger but that didn't help. How do I get rid of the extra space?

0条回答
登录 后发表回答