jframe中的java J面板中心位置

k3fezbri  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(159)

在jframe中放置jpanel的正确方法是什么?我曾尝试使用borderlayout.center作为jframe.add(jpanel,borderlayout.center)中的第二个参数。我还尝试在jpanel对象和setalignmentx((width+50)/2)中设置边界(20,20,width,height)。但是这些都不起作用,上面的每一个都被忽略了,所以jpanel被放置在左上角,与窗口面板重叠。我还没有找到解决堆栈溢出的有效方法。
我想把游戏板放在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();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题