Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Having a hard time with an GridBagLayout exercise in a course im following GridBagLayout  RSS feed

 
Francois Seguin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



t.jpg
[Thumbnail for t.jpg]
Here is the result im looking for
 
Francois Seguin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if somebody can figure out what im doing wrong...

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What you do in grid bag is to apply a set of constraints whenever you add a component.What you are doing is adding everything, then later setting the constraints.

Set constraints. Add something.
Change constraints. Add something.
Change constraints. Add something.

Don't write getContentPane(). It is unnecessary.
 
Francois Seguin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Ive done the changes and still no difference.
can somebody test this code for me and tell me what im doing wrong
i just cant get the panel to alligne like the sketch in my first post
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francois Seguin wrote:
can somebody test this code for me and tell me what im doing wrong

I can't speak for anyone else, but that's not generally how it works here. Show your code and explain how its behavior differs from what you expected. Maybe ask a specific "why did it do this particular thing I didn't expect" question. Or check out http://www.coderanch.com/how-to/java/HowToAskQuestionsOnJavaRanch (maybe start with "Tell the Details").

Campbell Ritchie wrote:Don't write getContentPane(). It is unnecessary.

I almost always include the getContentPane() bit. I guess I'm old school.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the code so I used labels and managed to get them to come out different colours. I cannot see what is wrong with your code. The only thing is that you have C 2 unitrs tall and E 2 units tall when they should be 1 and 3. Apart from that there seems to be nothing wrong with it.
Screenshot-from-2015-08-05-11-43-16.png
[Thumbnail for Screenshot-from-2015-08-05-11-43-16.png]
Screenshot 1
Screenshot-from-2015-08-05-11-43-37.png
[Thumbnail for Screenshot-from-2015-08-05-11-43-37.png]
Screenshot 2: different frame size
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you find Cay Horstmann's GBC class instead of that method. The many parameters make method calls really difficult to read. There is a fuller explanation in his Core Java II book (vol I).
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because A is 3 units tall, that may compel everything else to occupy three units. Try making A 4 units tall.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!