This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I am a new programmer and doing an assignment for my Java class. I think I am using the right layout, but the components aren't showing up right. They show up in a line all across the top of the applet. I am using the BorderLayout. Please let me know what I am doing wrong. Thank you!
Ulf Dittmer wrote:With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".
I'll move this to the AWT/Swing forum, as there seems nothing applet-specific to the question.
Then I must just be doing it completely wrong. I am trying to make an applet.
Joined: Mar 22, 2005
What I meant by that is: the question is about layouts, and thus GUIs and Swing in general - which are the same for both applications and applets. The fact that this code is used in an applet makes no difference.
With Swing you don't call "add()" or "setLayout(...)" directly, you'd call those methods on the content pane, like "getContentPane ().add(...)".
You don't need getContentPane(). JDK 5 supports this. These methods of a JFrame are directed to the content pane of the frame.
Please let me know what I am doing wrong
The code provided looks fine, so the problem is somewhere else.
Post your SSCCE that demonstrates the problem. And we don't care about your real panels. Just create a simple program that adds 5 different components to each area of the BorderLayout. So your SSCCE should basically be 5 lines of code in the init() method:
There is no need to set the layout specifically since a BorderLayout is used for all top level containers.