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.