I want to set the size of my JFrame to a specific height and width. However, I don’t want the border to be included in the sizing calculations. I want to size the window based off the content of the window.
For example, if I want my content panel to be 800x600, calling this doesn’t work:
this.setSize(800, 600); // “this” is the frame
It sets the size, but the content area has about 30 pixels cut off from the height and 6 or so from the width.
I also tried
this.getContentPane().setSize(800, 600);
And that didn’t work either. That just made the JFrame the minimum possible size. My understanding is that adding this.pack(); should solve that problem, but I must be misunderstanding what pack() does because it doesn’t seem to solve the problem at all.
I’ve also tried swapping out setSize with setPreferredSize, and also throwing in setMinimumSize, but nothing I do seems to work reliably.
Another related problem is that sometimes even when I manually set the size of the JFrame the contentpane doesn’t know what size it is. I’ll call this.getHeight() or something similar from within the contentpane and it returns 0.
What am I doing wrong here?

