Home > Net >  Resizing JPanel With BorderLayout
Resizing JPanel With BorderLayout

Time:03-15

I am trying to resize the center panel of my BorderLayout but the size is not changing. It keeps filling the rest of the frame that is available. I have tried setting the preferred size but has no effect. I would like how size of the frame but only need a portion of the center to be actually a panel for later use.

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class Gui extends JFrame {
    Border blackline = BorderFactory.createLineBorder(Color.black);
    private JPanel board;
    private JPanel buttons;
    private JButton setMissing, by4, by8;

    public Gui(){
        setUpGui();
    }

    public void setUpGui(){
        this.setSize(1000,1000);
        this.setTitle("Comp361 Assignment One");
        addButtons();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        board = new JPanel();

        board.setPreferredSize(new Dimension(400,400));
        this.add(board, BorderLayout.CENTER);
        //Sboard.setBackground(Color.Gray);
        board.setBorder(blackline);
        this.setVisible(true);
    }

    public void addButtons(){
        buttons = new JPanel(new FlowLayout());
        setMissing = new JButton("Set X");
        by4 = new JButton("4 by 4");
        by8 = new JButton("8 by 8");
        buttons.add(setMissing);
        buttons.add(by4);
        buttons.add(by8);
        this.add(buttons,BorderLayout.PAGE_START);
    }

    public static void main (String[] args){
        new Gui();
    }
}

CodePudding user response:

For extra padding around the central panel, you might put it to a panel with GridBagLayout (with no constraint) to center it, then add the GBL panel to the CENTER of the BorderLayout.

  • Related