3

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

Nora Powers
  • 1,597
  • 13
  • 31
  • Sounds more likely the problem is in the `BaseGamePanel`. `pack` simply uses the preferred size content pane to determine the size the frame should be (more or less) – MadProgrammer Jan 30 '13 at 02:55
  • I've been stepping through the program for quite a while now, and the `preferredSize` of the panel shows as `600, 600`, but the actual `width` and `height` become `610` once `pack` is called.. – Nora Powers Jan 30 '13 at 03:25
  • Don't forget that the frame may add in additional space to deal with the frame. What is the preferred size of the content pane (after pack)? Also, are you adding any borders to anything?? – MadProgrammer Jan 30 '13 at 03:27
  • The preferred size never changed; calling `setResizable(false)` before `pack()` was the issue. – Nora Powers Jan 30 '13 at 03:37

1 Answers1

8

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

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • That worked... Are the insets modified when `setResizable(false)` is called? – Nora Powers Jan 30 '13 at 03:28
  • 2
    @mmnumbp: Honestly, I'm not sure why it's important to do this; I just know that that's how it works. – Hovercraft Full Of Eels Jan 30 '13 at 03:35
  • 2
    @HovercraftFullOfEels Wow, would never have thought that would have been an issue. Learn something new every day +1 – MadProgrammer Jan 30 '13 at 04:31
  • The reason to call `setResizable(false)` before `pack()` is that the window 'chrome' (decorations around the edges of the window) might change between resizable and non resizable modes. By packing the window after the resizable property is set, the API will account for the (typically thinner) chrome used for a non-resizable window. – Andrew Thompson Aug 05 '15 at 02:10
  • Hope you don't mind me 'dropping in' to an answer I saw and up voted over two years ago, but I just was on a [different thread](http://stackoverflow.com/q/31822144/418556) where this one was linked, and the OP needed a slight clarification of the advice. Feel free to edit that into the answer (if you see fit). :) – Andrew Thompson Aug 05 '15 at 02:12