Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

panel.setSize() Not Working

 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Frame of Size 300 X 300. In this frame i have two panels, one of size 300 x 100 and other of 300 x 200. The first panel has a Button in the Center while the other panel has 9 buttons arranged in a Grid.

For doing this, i am creating a frame with size 300 x 300 and two panels, one with size 300 x 100 and other with 300 x 200. The problem is when i use panel.setSize(), it is not creating any effect. Below is the code that i have written.



Where am i going wrong?
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridLayout totally ignores the component's size.
From the API docs for GridLayout
The container is divided into equal-sized rectangles, and one component is placed in each rectangle


To fix your problem,
1) Change from GridLayout to FlowLayout
2) For the panels, use setPreferredSize() instead of setSize().

PS. In your code you haven't set the preferred size for the first panel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic