Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why is my JPanel too big?  (Read 4697 times)
0 Members and 1 Guest are viewing this topic.
Offline Ultraviper

Junior Newbie

« Posted 2009-01-29 07:52:52 »

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.
Offline ctrl-alt-dev

Senior Newbie

Today, my bedroom. Tomorrow the World !

« Reply #1 - Posted 2009-01-29 08:50:24 »

It depends on what layout manager you're using on the component your adding your panel to.
See this:

The quick fix might actually be to use
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][url]
Offline SunshineKiller

Junior Devvie

« Reply #2 - Posted 2009-01-29 14:34:37 »

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

<b>Check out my Development Blog:</b> <a href="">Scotts Creations</a> | <b>Games in Development: </b> <a href="">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

« Reply #3 - Posted 2009-01-29 15:39:14 »

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

Offline Ultraviper

Junior Newbie

« Reply #4 - Posted 2009-02-02 06:00:56 »

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.
Pages: [1]
  ignore  |  Print  

hadezbladez (1143 views)
2018-11-16 13:46:03

hadezbladez (514 views)
2018-11-16 13:41:33

hadezbladez (1176 views)
2018-11-16 13:35:35

hadezbladez (261 views)
2018-11-16 13:32:03

EgonOlsen (2554 views)
2018-06-10 19:43:48

EgonOlsen (2773 views)
2018-06-10 19:43:44

EgonOlsen (1556 views)
2018-06-10 19:43:20

DesertCoockie (2259 views)
2018-05-13 18:23:11

nelsongames (2109 views)
2018-04-24 18:15:36

nelsongames (2803 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!