0

I want to have a jframe with multiple jpanels within them. I use the gridbag, but every time I resize the frame, everything within it will be smooched and streched to fit the JFrame. I just want to drag and drop the size of the JFrame, but the images on the JPanel to stay the exact same size. (so revealing more or less, but always staying the same dimension).

Is there any way to do that? (while code is not needed since this is not a specific problem, here is the code anyways. I want the grid to always stay the exact dimensions. setSize and setPreferableSize did not work)

public class SetupBoard extends JFrame {
public SetupBoard() {
    int resolution = 10;
    System.out.println("This is the SetUpBoard");
    setSize(800, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);

    
    
// setup different Panels //

    // BlackPiecePanel //
    JPanel bPieceTaken = new JPanel();
    JLabel bPieceTakenText = new JLabel("Black Pieces");
    bPieceTaken.add(bPieceTakenText);

    // WhitePiecePanel //
    JPanel wPieceTaken = new JPanel();
    JLabel wPieceTakenText = new JLabel("White Pieces");
    wPieceTaken.add(wPieceTakenText);

    // ChessBoardPanel //
    JPanel chessBoard = new JPanel(new GridLayout(8,8));
    chessBoard.setSize(new Dimension(300,300));

    
    JLabel[] squares = new JLabel[64];
    for (int i = 0; i < 64; i++) {
        squares[i] = new JLabel();
        squares[i].setBorder(BorderFactory.createLineBorder(Color.black));
        chessBoard.add(squares[i]);
    }
    
    
    // Empty Panel left & right //

    JPanel ePLeft = new JPanel();
    JLabel ePLeftText = new JLabel("empty Panel left");
    ePLeft.add(ePLeftText);

    JPanel ePRight = new JPanel();
    JLabel ePRightText = new JLabel("empty Panel right");
    ePRight.add(ePRightText);
    ePRight.setBackground(Color.RED);

    
    
    
    // Construct Layout //

    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    
    // left Side //
    gc.weightx = 0.1;
    gc.weighty = 1.0;

    gc.gridx = 0;
    gc.gridy = 1;

    add(ePLeft, gc);
    
    // middle //
    gc.weightx = 0.8;
    gc.weighty = 0.1;
    
    gc.gridy = 0;
    gc.gridx = 1;
    add (bPieceTaken, gc);
    
    
    gc.weighty = 0.8;
    gc.gridy = 1;
    gc.fill = GridBagConstraints.BOTH;
    add (chessBoard, gc);
    
    gc.weighty = 0.1;
    gc.gridy = 2;
    gc.fill = GridBagConstraints.BASELINE;
    add (wPieceTaken, gc);
    
    
    // right Side //
    gc.weightx = 0.1;
    gc.weighty = 1;
    
    gc.gridx = 2;
    gc.gridy = 1;
    add (ePRight, gc);
    

    setVisible(true);

    System.out.println (chessBoard.getSize());
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Razak
  • 43
  • 6
  • 1
    For a Board Game, see [Making a robust, resizable Swing Chess GUI](https://stackoverflow.com/q/21142686/418556). This does not prevent the panels of the board from resizing, instead, it resizes them to the biggest they can be while still square. – Andrew Thompson May 05 '22 at 10:28
  • Thank you! But that is not what I want. I want it to always stay a certain size, no matter how big the frame is. – Razak May 05 '22 at 11:53
  • *"I want it to always stay a certain size, no matter how big the frame is."* A single component (such as a `JPanel` with a `GridLayout` for a board game) will be centered in a `GridBagLayout` if added as the only component, and with no constraint. No matter how big the parent container is, *it will remain the same size.* See [this answer](https://stackoverflow.com/a/7181197/418556) for an example. – Andrew Thompson May 05 '22 at 12:12
  • I've tried that. But it only takes the minimal amount of size needed to show the panel. So if it is just a Label, it is just like 20x50. And I want it to be a set size, not just the absolute minimum needed. And even if I tell the JPanel I put in there what size it should be, it doesnt seem to matter. – Razak May 05 '22 at 12:31
  • *"So if it is just a Label, it is just like 20x50. And I want it to be a set size, not just the absolute minimum needed."* Add an empty border to the label. There are a number of things you could do here, but you keep gradually tweaking the requirements, so it's hard to guess (and I have better things to do). – Andrew Thompson May 05 '22 at 13:11

1 Answers1

1

You can try to build your own LayoutManager, and then you will have full control of how your components resized and where they positioned every time your container resized. You should do this by creating a class which implements the LayoutManger interface, and implement the method within it as you need, according to your design.

If you haven't done this already, you can see this oracle article on this subject:
Creating a Custom Layout Manager

Programmer
  • 803
  • 1
  • 6
  • 13