• Post Reply Bookmark Topic Watch Topic
  • New Topic

BorderLayout Problems

 
John Ab
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to write a small Java program to experiment around with BorderLayout, because I'm developing a Java game and I need to have 2 objects placed in a single JFrame at the same time, and everyone I asked said I need BorderLayout to do that.

Before you answer: Also, is using BorderLayout the best option for adding multiple video game oriented objects such a sprites to a JFrame?


So the Java program I wrote is supposed to place a JButton on the JFrame and ALSO place a graphic component (a rectangle in this case). The problem is, only the button shows up, as can be seen in the image link below:

http://prntscr.com/3m5ek6


Here is the code:


**main.java** --> The main method class + JFrame/JPanel/JButton constructor


**Infout.java** --> Rectangle constructor class




Can someone tell me what's wrong?

Thanks!

Ab
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BorderLayout uses the preferred size of a component to size the various areas but your Infout object has no preferred size set so will return a size of 0 for both width and height hence you can't see it. If you add a default constructor and call setPreferredSize(new Dimension(145, 570)); you will see filled in rectangle.

To answer your other question a layout manager does just that ie it lays out components, it doesn't care what the components are or what they do it just positions and sizes them. The question really is does BorderLayout give you the layout you require. Note: there are many layout managers and almost all will layout multiple components.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me what's wrong?


You already have the answer. It was given to you by two different people when you posted this question in another forum.

Cross posted: http://stackoverflow.com/questions/23847889/borderlayout-for-objects

This question has been asked and answered twice!!!

It is annoying when you continue to waste the time of people who generously donate their time to answer questions on these forums.

@Tony, the answer given in the other posting was to override the getPreferredSize() method to return the Dimension. The component itself should be responsible for determining the preferred size, not the application program. The OP was also given a link to the Swing tutorial on Custom Painting which contains a working program showing how to do this.
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for letting us know about the cross posting.
John Please BeForthrightWhenCrossPostingToOtherSites (← click) for the reasons Rob has given.

@Tony, the answer given in the other posting was to override the getPreferredSize() method to return the Dimension. The component itself should be responsible for determining the preferred size, not the application program

Quite agree, I guess my solution wasn't clearly worded. I intended the OP add the default constructor to the Infout class so the component was setting it's own preferred size.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!