Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: The layout of GUI

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The layout of GUI

    layout.jpg
    I am working on some basic questions about GUI. The above picture is the required interface. However, part of my work is not ideal.

    mylayout.png
    This is my work. I use a border layout to contain all the other panels. However, the north panel and east panel are not identical with the required one. What wrong with my code?

    import java.awt.*;
    import javax.swing.*;
     
    public class Exercise2 {
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(420, 250);
    		frame.setTitle("Layout question");
    		frame.setLayout(new BorderLayout());
     
    		JLabel label = new JLabel("Buttons:");
    		JButton b1 = new JButton("hi");
    		JButton b2 = new JButton("long name");
    		JButton b3 = new JButton("bye");
    		JPanel northPanel = new JPanel(new FlowLayout());
    		northPanel.add(label);
    		northPanel.add(b1);
    		northPanel.add(b2);
    		northPanel.add(b3);
    		frame.add(northPanel, BorderLayout.NORTH);
     
    		JCheckBox boldBox = new JCheckBox("Bold");
    		JCheckBox italicBox = new JCheckBox("Italic");
    		JCheckBox underlineBox = new JCheckBox("Underline");
    		JCheckBox strikeoutBox = new JCheckBox("Strikeout");
    		JPanel westPanel = new JPanel(new GridLayout(4, 1));
    		westPanel.add(boldBox);
    		westPanel.add(italicBox);
    		westPanel.add(underlineBox);
    		westPanel.add(strikeoutBox);
    		frame.add(westPanel, BorderLayout.WEST);
     
    		JButton b4 = new JButton("1");
    		JButton b5 = new JButton("2");
    		JButton b6 = new JButton("3");
    		JButton b7 = new JButton("4");
    		JButton b8 = new JButton("5");
    		JButton b9 = new JButton("6");
    		JButton b10 = new JButton("7");
    		JPanel subPanel = new JPanel(new GridLayout(2, 2));
    		subPanel.add(b6);
    		subPanel.add(b7);
    		subPanel.add(b8);
    		subPanel.add(b9);
    		JPanel centerPanel = new JPanel(new GridLayout(2, 2));
    		centerPanel.add(b4);
    		centerPanel.add(b5);
    		centerPanel.add(subPanel);
    		centerPanel.add(b10);
    		frame.add(centerPanel, BorderLayout.CENTER);
     
    		JButton closeButton = new JButton("Cancel");
    		frame.add(closeButton, BorderLayout.SOUTH);
     
    		frame.setVisible(true);
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The layout of GUI

    You can obtain the desired effect by setting the preferred sizes of the components in the north panel, changing the layout to flow layout and setting the preferred sizes of the check boxes in the west panel, and then packing the frame before setting it visible.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The layout of GUI

    Quote Originally Posted by GregBrannon View Post
    You can obtain the desired effect by setting the preferred sizes of the components in the north panel, changing the layout to flow layout and setting the preferred sizes of the check boxes in the west panel, and then packing the frame before setting it visible.
    Thank you for your reply. I tried to change the layout of west panel to flow layout before. However, the components are arranged horizontally. The second question is when I set the preferred sizes of those components, is there any way to know the value of the desired dimension? Now, I just set the size like
    b1.setPreferredSize(new Dimension(100, 50));
    Is there any more efficient way to set the size?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The layout of GUI

    However, the components are arranged horizontally.
    It will still work for the size/orientation shown, and it can be forced even if resized by setting the preferred size of the west panel.
    Is there any more efficient way to set the size?
    Ultimately, no, but you can simplify experimenting by using constants set in one location and relating them mathematically when possible.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The layout of GUI

    Quote Originally Posted by GregBrannon View Post
    It will still work for the size/orientation shown, and it can be forced even if resized by setting the preferred size of the west panel.

    Ultimately, no, but you can simplify experimenting by using constants set in one location and relating them mathematically when possible.
    I still don't understand what you said about the west panel.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The layout of GUI

    Have you tried setting the preferred size for the components? If so, show that code and describe what is still not satisfactory. Mine looks ~exactly like it's supposed to, so I'm not sure what else to tell you except to go do what I suggested and come back with code if it's not working for you.

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The layout of GUI

    Quote Originally Posted by GregBrannon View Post
    Have you tried setting the preferred size for the components? If so, show that code and describe what is still not satisfactory. Mine looks ~exactly like it's supposed to, so I'm not sure what else to tell you except to go do what I suggested and come back with code if it's not working for you.
    Thank you for your reply. I think I found the problem. I forgot to set the preferred size of the west panel. Thank for your guidance.

Similar Threads

  1. GUI Layout - Does anyone know how to set a layout?
    By mikemontesa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 20th, 2013, 04:35 PM
  2. Noob at GUI's, need layout help.
    By tyb97 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 2nd, 2011, 03:50 PM
  3. Is there a layout for this?
    By gkffjcs in forum AWT / Java Swing
    Replies: 2
    Last Post: September 27th, 2011, 09:15 AM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM