简体   繁体   English

在我调整JFrame大小之前,JPanel没有完全显示

[英]JPanel doesn't show up completely until I resize JFrame

I am making a board game and need two panels on a JFrame. 我正在制作棋盘游戏,需要在JFrame上使用两个面板。 I added them and they look good enough but one of them, the big grid to play the game, doesn't show all the way, I only see a little, I have to resize the window manually. 我添加了它们,它们看起来不错,但其中一个,玩游戏的大网格,并没有一直显示,我只看到一点,我必须手动调整窗口大小。

I have tried with different layouts and nothing works. 我试过不同的布局,没有任何作用。 This is the code in which I add the components to the frame: 这是我将组件添加到框架的代码:

    panel.setSize(500, 100);
    panel.setBackground(Color.WHITE);
    panel.add(botonArriba);
    panel.add(botonAbajo);
    panel.add(botonIzquierda);
    panel.add(botonDerecha);
    panel.setVisible(true);

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    tablero.setSize(281, 300);
    this.add(tablero, BorderLayout.CENTER);
    this.add(panel, BorderLayout.SOUTH);
    this.setTitle("2048");
    this.pack();
    this.setVisible(true);

Use setPreferredSize(...) which is what the layout manager tries to achieve. 使用setPreferredSize(...) ,这是布局管理器尝试实现的。 Of course the panel needs a LayoutManager too. 当然面板也需要LayoutManager。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM