Results 1 to 8 of 8
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Question 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you pit one button in the north and the other in the center?

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    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

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  8. #8
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    I'll have a good look at that tutorial and post back in a bit if theres something I still don't understand.
    Thanks very much for your help guys!

Similar Threads

  1. Simple GUI layout manager
    By globo in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 01:17 AM
  2. Question about Layout
    By hei1233212000 in forum New To Java
    Replies: 6
    Last Post: 11-04-2010, 01:42 PM
  3. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 08:51 PM
  4. Layout Question
    By vittoire in forum New To Java
    Replies: 6
    Last Post: 01-19-2010, 02:11 PM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 07:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •