Why is my JPanel too big?

I’m trying to make my first game and I’m having a strange problem. In my panel class I have


	private static final int WIDTH = 640;
	private static final int HEIGHT = 480;

and I use these for both creating the panel


setPreferredSize(new Dimension(WIDTH, HEIGHT));

and creating an image to draw to the screen.


image = createImage(WIDTH,HEIGHT);

But for some reason when I run the program it makes the panel 650 by 490 which is 10 pixels too big in both directions. I have no idea what is causing this. I have fixed it by changing the panel constructor to

setPreferredSize(new Dimension(WIDTH-10, HEIGHT-10));

but I don’t understand why I need to be doing such a thing.

I’ve tried using different dimensions and no matter what I pick, both sides are always 10 pixels too big, except that the width never goes below 118. When I tried WIDTH = 1 and HEIGHT = 1, I ran my program and it gave me a panel that was 118 by 11.

It depends on what layout manager you’re using on the component your adding your panel to.
See this: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

The quick fix might actually be to use
setMinimumSize(…)
setMaximumSize(…)
as well. If that does not work you can try to set the layout manager on the container of your panel to null and then use setLocation(…) and setSize(…) to place the component manually. It won’t resize with the window through.
See this: [url]http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html[url]

for a easy to use layout manager you can try MigLayout google it, its really easy :slight_smile:

I can’t tell from the code you have shown. If you show your complete code there might be something obvious.

I think I found the problem.
Calling setResizable after calling pack() within the JFrame constructor made the JPanel bigger than it needed to be. Still not sure why or how the frame would add extraneous pixels to my panel dimensions but it’s fixed.