Results 1 to 8 of 8
Thread: Simple Layout Question
- 07-13-2011, 12:21 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Simple Layout Question
Hello, I have just recently started learning about JButtons, panels etc. What I am struggling with is getting them to be positioned on the screen where I want them.
Here is a very simple code which currently displays 2 buttons next to each other in the top left corner of the screen. My question is: how would I get the second button to be directly below the first one?
import java.awt.*;
import javax.swing.*;
public class LayoutTest extends JFrame {
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JPanel p = new JPanel();
public LayoutTest() {
JFrame f = new JFrame("Layout");
f.setSize(500, 420);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.add(b1);
p.add(b2);
f.add(p, BorderLayout.WEST);
f.setVisible(true);
}
public static void main(String[] args) {
LayoutTest lT = new LayoutTest();
}
}
I tried to work it out by reading tutorials on BorderLayouts and stuff but I don't really understand them at all.
- 07-13-2011, 12:55 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
When you add a button(or any component) to a borderlayout you have to also provide a position. Positions can be NORTH, CENTER, SOUTH, WEST, EAST. Try playing around with adding the buttons to different sections and see what happens.
- 07-13-2011, 01:02 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
I tried doing p.add(b1, BorderLayout.SOUTH);
but its position didn't change at all.
I also tried using a second panel for the second button and setting its position to west as well, since I want it under the first button, but it was placed on top of button 1 so I couldn't even see the first button anymore.
I have tried experimenting quite a bit before posting this question and honestly can't figure out how to do it.
If you have the time could you please change my code so that it works (if its just a simple modification).
Sorry if I'm being thick
Edit: I have managed to make 5 buttons (north, west, east, south and centre) and put them in their correct locations, my problem is having more than one button in an area.
- 07-13-2011, 01:24 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if you pit one button in the north and the other in the center?
- 07-13-2011, 01:40 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
If I make two different panes for each button and add each pane to the main frame with the first North and the second Center then I get nearly what I want - two buttons below each other in the center. What I want in the end is to have one column of 4 radio buttons down the west with JLabels next to each one, and then the same thing except with checkboxes on the East side.
I tried adding the two panels to another pane and setting its location to west so that I would have the two buttons below each other on that side, but instead it just put the two buttons next to each other at the top. I'm also not sure how i could do 4 buttons using North and Center.
I'm not sure if I'm making sense, but I hope so...
Thank you very much for your help so far.
- 07-13-2011, 01:45 PM #6
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Remember that you can add a container to another container, aka nesting layouts.
db
- 07-13-2011, 01:51 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That link Darryl posted will be incredibly helpful. I find that layout manners take some experimentation when you first start. Boxes may be helpful for some of the things you mentioned.
- 07-13-2011, 01:57 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Similar Threads
-
Simple GUI layout manager
By globo in forum New To JavaReplies: 3Last Post: 11-16-2010, 01:17 AM -
Question about Layout
By hei1233212000 in forum New To JavaReplies: 6Last Post: 11-04-2010, 01:42 PM -
very simple Question
By arsenal4ever_11 in forum NetBeansReplies: 2Last Post: 05-27-2010, 08:51 PM -
Layout Question
By vittoire in forum New To JavaReplies: 6Last Post: 01-19-2010, 02:11 PM -
Simple Question
By barusk in forum NetworkingReplies: 13Last Post: 03-04-2009, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks