Actual Size of a JFrame?

Why when I do:

[quote]gameFrame.setPreferredSize(new Dimension(800, 600));
[/quote]
It gives me a frame about 10 pixels less on the left and the right :confused:

I have done setSize method as well to no prevail.

How can I fix this so the graphics component embedded in the JFrame is actually 800 x 600


http://img62.yfrog.com/img62/3504/blahblaha.png

Why? Because the size includes the border area. Add a bit for the border edges. I’m not sure how to determine exactly how much. It might change based upon different “look & feels”. I usually add 32 to the y-dim (JFrame vs JPanel to display on it).

Call setPreferredSize on the component embedded in the JFrame instead of the JFrame itself.

Then call JFrame.pack().

I tried that and it didn’t work =/

I want it to be exact so if they game were ever to go fullscreen it wouldn’t have weird offsets, and I feel like it should fit to the size I specify. Stupid swing.

Did you set the preferred size of your embedded component, call pack on the JFrame and then do frame.setVisible(true) ? I seem to remember having some difficulty a while ago with the order of things in Swing. This is how I would do it, replacing the JPanel with whatever your embedded component is.


JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
panel.setPrefferedSize(new Dimension(800,600));
frame.add(panel);
frame.pack();
frame.setVisible(true);

How about running this code after calling setVisible(true):


Insets i = frame.getInsets();
frame.setSize(width+i.right+i.left,height+i.bottom+i.top);

Genius, thanks! You should add me on AIM or skype, I seem to run into you a lot :smiley:

Haha, glad to help :smiley:

EDIT: Much cleaner code would be to set the preferredSize on the contentPane of the JFrame:


...
frame.getContentPane().setPreferredSize(new Dimension(width,height));
frame.pack();
frame.setVisible(true);
...

Ah, “insets” – so that is how you get the size inside the border area.
Thanks ra4king.