Java – why call JFrame Will pack () add extra space?

At first, the code I used worked well, but it was a little confusing After moving some parts of the method to the constructor of JFrame, things are normal

Everything except using pack () makes the frame the right size

This is the original code:

public class BaseGameFrame extends JFrame {

public static final int WINDOWED = 0;
public static final int UFS = 1;

protected BaseGamePanel gamePanel;

public BaseGameFrame(String title,int pWidth,int pHeight,long period,int windowType){
    super(title);

    switch(windowType){
        case UFS:
                    this.setUndecorated(true);

                    Rectangle screenSize = this.getGraphicsConfiguration().getBounds();
                    pWidth = screenSize.width;
                    pHeight = screenSize.height;

                    break;

        default:    break;
    }

    this.setVisible(true);
    this.setResizable(false);
    this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

    this.createPanel(pWidth,pHeight,period);


}

protected void createPanel(final int pWidth,final int pHeight,long period){
    this.gamePanel = new BaseGamePanel(pWidth,period);

    this.add(this.gamePanel);
    this.pack();
}

public static void main(String[] args){
    new BaseGameFrame("Test",800,600,20L * 1000000L,UFS);
}

}

This is after modifying it:

public class BaseGameFrame extends JFrame {


protected BaseGamePanel gamePanel;

public BaseGameFrame(String title,VideoType vType,BaseGamePanel gp){
    super(title);

    switch(vType){
        case UFS:   
                    this.setUndecorated(true);

                    Rectangle screenSize = this.getGraphicsConfiguration().getBounds();
                    gp.setPDimensions(new Dimension(screenSize.width,screenSize.height));

                    break;

        default:    break;
    }


    this.add(gp);
    this.pack();

    this.setVisible(true);

    this.setResizable(false);
    this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);


}

public static void main(String[] args){
    BaseGamePanel gp = new BaseGamePanel(800,20L * 1000000L);
    new BaseGameFrame("Test",VideoType.UFS,gp);
}

}

I'm not sure what the problem is... But what happened in the end is:

Solution

Ensure that setResizable (false) is invoked before calling pack () or setVisible (true).

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>