Search code examples
javaswingjframejpanelsizing

Using a JPanel inside JFrame does not size or move correctly


thank you for taking your time in reading this. I hope it is not confusing, and if any other information is needed, please let me know!

Issue: I am making a Snake like game and I've created a JFrame, and then added the JPanel. Well, if I do not set the size of it in the JFrame it will size to about 100 x 20. When I set the size of it, it stays at the dimensions set. However, if I pack() it, it will shrink back to the 100 x 20 or so, when I have defined the size in the JPanel. I've set the bounds as well, with no luck. I have use FlowLayout(), and BorderLayout() with no success either. Now my game runs correctly and have no issues with it, except for the frame. It will cut off the sides, unless i do some gymnastics, which I do not wish to do.

Objective: I would like to be able to have the Frame size correctly to my width and height specified, and have the panel moved down a bit to add space for text for the game so it is not blocking anything.

Basically I'm confused and flustered that I have spent a few days on this, and I have not been successful with anything so far.

JFrame Class:

public class main extends JFrame {

 /**
  * @param args the command line arguments
  */

 public main(){
     Board game = new Board();

     setLayout(new BorderLayout(0, 50));
     setSize(game.getGameWidth(), game.getGameHeight());
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);
     setTitle("BlockRunner");
     getContentPane().add(game);

     setResizable(false);
     setVisible(true);
 }
}

My Board class:

public class Board extends JPanel implements ActionListener, KeyListener{
 private int width = 600;
 private int height = 400;
 Board(){
     addKeyListener(this);
     setFocusable(true);
     setBackground(Color.LIGHT_GRAY);
     setSize(width, height);
     InitGame();
 }
}

I am willing to try anything someone is willing to put out there for me. Thank you for your time!

EDIT 1: (Due to a user's answer.) using setPerferredSize(), setMinmumSize() and setMaximumSize() does not affect the outcome of this.


Solution

  • You need to use a combination of getPreferred/Minimum/MaximumSize and a layout that will actually respect it.

    Here, I've used a GridBagLayout set up so that the board will always stay the same size, but the cell will use the maximum amount of same.

    If you want to keep the text at the bottom of the board (instead of the frame), remove the weighty constraint (and possibly the weightx) as well

    enter image description hereenter image description here

    public class TestBoardGame {
    
        public static void main(String[] args) {
            new TestBoardGame();
        }
    
        public TestBoardGame() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    gbc.weightx = 1;
                    gbc.weighty = 1;
                    frame.add(new BoardGamePane(), gbc);
    
                    gbc.gridx = 0;
                    gbc.gridy = 1;
                    gbc.weightx = 1;
                    gbc.fill= GridBagConstraints.HORIZONTAL;
                    JLabel text = new JLabel("Area for text");
                    text.setHorizontalAlignment(JLabel.CENTER);
                    frame.add(text, gbc);
    
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class BoardGamePane extends JPanel {
    
            public BoardGamePane() {
    
                setBorder(new LineBorder(Color.RED));
                setBackground(Color.ORANGE);
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
    
            @Override
            public Dimension getMinimumSize() {
                return getPreferredSize();
            }
    
            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
    
        }    
    }