Whats the correct way to position a Jpanel in Jframe? I have tried to use BorderLayout.CENTER as a second arg in jframe.add(jpanel, BorderLayout.CENTER). I have also tried to setBounds(20, 20, width, height) in jpanel object and setAlignmentX((width+50)/2). But none of these worked, each of the above was ignored so the jpanel was positioned in the upper left corner, overlapping the window panel. I havent found a working solution on stackOverflow.
I want the gameboard positioned in a center of a jframe.
public class MainBoard extends JFrame {
public MainBoard(String title,int width, int height){
setTitle(title);
setSize(new Dimension(width+50, height+50));
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
createBufferStrategy(2);
GameBoard g = new GameBoard(width, height, getBufferStrategy());
add(g);
}
}
public class GameBoard extends JPanel implements Runnable {
private final int WIDTH;
private final int HEIGHT;
private boolean inGame;
private final BufferStrategy bs;
private final int FRAME_DELAY = 50;
private long cycleTime;
public GameBoard(int width, int height, BufferStrategy bs) {
addKeyListener(new TAdapter());
setFocusable(true);
setIgnoreRepaint(true);
WIDTH = width;
HEIGHT = height;
this.bs = bs;
gameInit();
}