1

I created a JFrame with BorderLayout and one Panel on the east (blue) and one in the center(green. The center panel contains an orange panel which will always stay a square.

I just cant figure out why the organge panel always has a gap at the top (also after resizing) because I did not set any gaps at all. This is how it looks and this is what I tried:

    public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(600+200, 600));
        frame.setLocationByPlatform(true);
        BorderLayout borderLayout = new BorderLayout();
        frame.setLayout(borderLayout);
        
        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(Color.BLUE);
        rightPanel.setPreferredSize(new Dimension(200, 600));
        frame.add(rightPanel, BorderLayout.EAST);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.GREEN);

        SquarePanel squarePanel = new SquarePanel();
        squarePanel.setBackground(Color.ORANGE);
        centerPanel.add(squarePanel);

        frame.add(centerPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}


    public class SquarePanel extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        Dimension d;
        Container c = getParent();
        if (c != null) {
            d = c.getSize();
        } else {
            return new Dimension(10, 10);
        }
        int w = (int) d.getWidth();
        int h = (int) d.getHeight();
        int s = (w < h ? w : h);
        if(s%8 != 0){       // make Dimensions stay multiples of 8
            s = s - s%8;
        }
        return new Dimension(s, s);
    }
}

Unfortunately, boarderLayout.setVGap(0) does not work.

corneliu
  • 656
  • 13
  • 37
DerPascal
  • 43
  • 3
  • 2
    You should call `pack()` on the JFrame after adding components and before setting it visible. And you shouldn't be setting the size or any size hints for the frame. Let the JFrame size itself. – Hovercraft Full Of Eels Dec 20 '21 at 13:24
  • But the orange panel has to have sizes that are multiples of 8 because its actually a chess board or did you mean something else – DerPascal Dec 20 '21 at 13:30
  • What does that have to do with setting the size or sizing hints of the JFrame? Again, let the JFrame size itself. The layout managers and components will tell it how to correctly size. – Hovercraft Full Of Eels Dec 20 '21 at 13:31
  • I tried what you said, but the gap is still there – DerPascal Dec 20 '21 at 13:38
  • *"a chess board"* For a 'no gaps' (except where intended as white-space) chessboard, see [this example](https://stackoverflow.com/a/21142687/418556). – Andrew Thompson Dec 21 '21 at 01:26

1 Answers1

4

Because if you don't give a layout to the centerPanel I believe the default layout is FlowLayout. FlowLayout automatically creates that padding around the elements. You need to give the centerPanel a border layout like this centerPanel.setLayout(new BorderLayout());. Panels do not inherit the layout of their parent.

Alternatively you may try to change the padding of FlowLayout like here Java FlowLayout - Margin/padding of specific elements?

corneliu
  • 656
  • 13
  • 37