简体   繁体   中英

JPanel doesn't show up completely until I resize JFrame

I am making a board game and need two panels on a 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. Of course the panel needs a LayoutManager too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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