Java 为什么我的组件只在调整窗口大小时显示?

Java 为什么我的组件只在调整窗口大小时显示?,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我需要在jpanel中使用gridlayout和JLabel制作一个棋盘。因此,这项工作目前还不错。但我的主窗口仅显示调整大小时的组件 问题是,如何让我的JPanel使用标签块的大小,然后让Jframe使用我的JPanel的大小 代码如下: public class MainWindow extends JFrame { public MainWindow(){ super(); this.setLayout(new BorderLayout());

我需要在jpanel中使用gridlayout和JLabel制作一个棋盘。因此,这项工作目前还不错。但我的主窗口仅显示调整大小时的组件

问题是,如何让我的JPanel使用标签块的大小,然后让Jframe使用我的JPanel的大小

代码如下:

public class MainWindow extends JFrame {

    public MainWindow(){
        super();
        this.setLayout(new BorderLayout());
        this.setLocationRelativeTo(null);
        addNewGameArea();
        this.pack();
        this.setVisible(true);
    }

    private void addNewGameArea(){

        GameArea game = new GameArea();
        this.add(game, BorderLayout.CENTER);
    }
}
另一类:

public class GameArea extends JPanel {

     public GameArea(){
        super();
        this.setLayout(new GridLayout(10,10));
        addLabels();            
        //this.setPreferredSize(this.getPrefferedSize());
        this.setVisible(true);


    }

    //Creates a 2D JLabel array
    private JLabel[][] createLabel(){

        JLabel[][] labelarray= new JLabel[10][10];
        for (int i=0;i < 10;i++){
            for (int j = 0; j < 10; j++){
                labelarray[i][j] = new JLabel();
            }
        }
        return labelarray;
    }

    // Color the labels alternating black and white
    private void colorLabels(JLabel[][] labelAr){

        //Here i set the color of the tiles, set them opaque and set the size to (50, 50)
    }

    // add the labels to the Panel
    private void addLabels(){
        JLabel[][] labels = createLabel();
        colorLabels(labels);

        for (int i=0;i<10;i++){
            for (int j = 0; j < 10; j++){
                this.add(labels[i][j]);
            }
        }
    }
}

这个例子对我来说似乎很好,也许还提供了
main
方法请你的问题包括更新的代码;另见。
public class StartGui {
    public static void main (String[] args){
        MainWindow mw = new MainWindow();
    }
}