Hi, I am trying to add a JPanel to the center region of a BorderLayout. The JPanel should be 200 by 200, but when I add it directly to the BorderLayout, it is stretched out. I need it to be just 200 by 200 pixels. I have tried adding another JPanel with a FlowLayout, and adding my JPanel to that, then adding that to the BorderLayout. But when I do that, my JPanel shrinks down to maybe 10 by 10 pixels. This is frustrating, I hate swing. It seems like it never works quite the way it's supposed to... Anyway, is there something I'm missing? Here's my code:
see if this gets you any closer
(had to create a DisplayArea - use it first to see the border outline, then remove the class, recompile with your own DisplayArea)
Joined: Jul 11, 2006
Thanks, that works. I changed the layout of centerPanel back to flowlayout though, because I don't understand gridbaglayout (yet). And yes, by the way I just have a separate class that starts up DrawString and the DisplayArea is just a JPanel. So really all I needed to do was change my JPanel code from setSize(200, 200) to setPreferredSize(new Dimension(200, 200)). Why should that make a difference? What is the difference between the two methods, and why doesn't it work when I use setSize? It seems like that's the method I should use... pretty annoying if you ask me.
Joined: Jun 09, 2003
layoutManager manage the size, trying to honor a component's preferredSize
in no layout manager (null) setSize will work