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());
}