Results 1 to 10 of 10
Like Tree3Likes
  • 3 Post By JosAH

Thread: Format of GUI

  1. #1
    winghorse is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default Format of GUI

    Format of GUI-layout.png
    I am working on some basic questions about GUI and the above picture is the required interface that I have to work out. However, my work is as below:

    Format of GUI-mylayout.png

    I use a border layout to contain all the other panels. However, the north panel and west panel in my work are not identical to those in the first picture. How to make it correct?

    Below is my code:

    Java 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);
    	}
    
    }
    Last edited by winghorse; 12-21-2013 at 06:55 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Format of GUI

    The four tick boxes on the left (west) could better be stored in a GridBagLayout, that doesn't make them stretch out vertically; the strip on the top probably can be achieved by sticking a JLabel ("buttons:") as the west component and the three buttons in another JPanel (with a GridLayout(1, 3) as the center component; (both in another JPanel with a BorderLayout).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Format of GUI

    I, for one, always prefer to use absolute positioning.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Format of GUI

    Quote Originally Posted by zFollette View Post
    I, for one, always prefer to use absolute positioning.
    Cool, and then see your code run on a screen with a different resolution ...

    kind regards,

    Jos
    gimbal2, SurfMan and liluma like this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: Format of GUI

    Quote Originally Posted by JosAH View Post
    Cool, and then see your code run on a screen with a different resolution ...

    kind regards,

    Jos
    Especially a lower one!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Format of GUI

    Quote Originally Posted by gimbal2 View Post
    Especially a lower one!
    Yep, and especially if there's a window manager in charge that allows the title bar go off screen ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    939
    Rep Power
    2

    Default Re: Format of GUI

    Quote Originally Posted by zFollette View Post
    I, for one, always prefer to use absolute positioning.
    In the past 15 years of Java programming, I have never, ever needed absolute positioning. If you are serious about a proper Java Swing GUI, you will most probably work with a combination of GridBagLayout, BorderLayout, JGoodies FormLayout, MigLayout. I for one, am a MigLayout lover. No GUI goes without one.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Format of GUI

    Ouch

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: Format of GUI

    Quote Originally Posted by SurfMan View Post
    you will most probably work with a combination of GridBagLayout, BorderLayout, JGoodies FormLayout, MigLayout.
    GridBagLayout? That to me is the same as using absolute positioning, you use it when you're only learning. Beyond that its just too much fiddling to get right. FormLayout looks exceptionally cool, I've never had the pleasure of using it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Format of GUI

    After seeing what you guys think of absolute positioning, I changed my most recent Swing application to a combination of the default BorderLayout and GridLayout. It works very well with minimal code.

Similar Threads

  1. Can't get the decimal format to format correctly...
    By Valerie1067 in forum New To Java
    Replies: 5
    Last Post: 03-25-2012, 04:15 AM
  2. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  3. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  4. Format
    By 2ndis1stplaceloser in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 06:38 AM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 03:37 AM

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
  •