Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is calling JFrame.pack() adding extra space?

Originally, the code I was using worked fine, but was a bit convoluted. After moving some parts of a method into the constructor for the JFrame, things were working properly.

Everything except using pack() to make the frame the proper size.

Here 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, pHeight, period);

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

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

}

and here it 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, 600, 20L * 1000000L);
    new BaseGameFrame("Test", VideoType.UFS, gp);
}

}

I'm not quite sure what the problem is.. but what ends up happening is this: this is an issue

like image 497
Nora Powers Avatar asked Dec 21 '25 09:12

Nora Powers


1 Answers

Make sure to call setResizable(false) before calling pack() or setVisible(true)

like image 181
Hovercraft Full Of Eels Avatar answered Dec 22 '25 23:12

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!