Results 1 to 6 of 6
  1. #1
    nandj987 is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Help making simple GUI prettier

    I was hoping for some help making this simple GUI prettier. It is rather ugly but I haven't quit figured out how to make my GUIs look nice yet. When I run the code below on a Windows PC, all of the stuff (buttons, labels, etc.) are all at the bottom of the window with a big white space gap at the top (all my GUIs seem to have this). I've dabbled with a "GridBagLayout" but I still always seem to end up with white space at the top of my GUIs. Any help or suggestions would be appreciated.


    Java Code:
    public class Installer implements ActionListener
    {
    	private JFrame MainFrame;
    	private JLabel installDirLabel;
    	private JButton changeDirBtn;
    	private JButton installBtn;
    	private JFormattedTextField installDirTextField;
    	private JPanel installerPanel;
    	private Set<String> installFiles;
    
    	public Installer()
    	{
    	    	JOptionPane.showMessageDialog(null,
        			"Thank you for installing!",
        			"Installation",
        			JOptionPane.PLAIN_MESSAGE);
    
    	    	MainFrame = new JFrame("Installation");
        		JFrame.setDefaultLookAndFeelDecorated(true);
    	    	MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		MainFrame.setSize(new Dimension(550, 200));  // size of window
    	    	MainFrame.setLocation(300,200); // location of window offset from upper-left
            	
    		installerPanel = new JPanel();
    		installerPanel.setBorder(BorderFactory.createCompoundBorder(
    	                BorderFactory.createLoweredBevelBorder(),
                 	   BorderFactory.createEmptyBorder(5,5,5,5)));
    
    	 	installDirLabel = new JLabel("Choose an installation directory:", SwingConstants.CENTER);
    		changeDirBtn = new JButton("Browse...");
    		installBtn = new JButton("Install");
            
    		installDirTextField = new JFormattedTextField();
    		installDirTextField.setColumns(30);
    		installDirTextField.setFont( new Font("Dialog", Font.PLAIN, 12));
    		installDirTextField.addActionListener(this);
    		installDirTextField.setEditable(true);
    	 	installDirTextField.setText("C:\\Program Files");
    
    		installerPanel.add(installDirLabel);
    		installerPanel.add(installDirTextField);
    		installerPanel.add(changeDirBtn);
    		installerPanel.add(installBtn);
            
    		installBtn.addActionListener(this);
    	 	changeDirBtn.addActionListener(this);
            
    	        MainFrame.getContentPane().add(installerPanel, BorderLayout.SOUTH);
    	        MainFrame.setVisible(true);
    	}
    	
    
    
    	private static void createAndShowGUI()
    	{
    	        JFrame.setDefaultLookAndFeelDecorated(true);
    
    	}
    
    
    	public static void main(String[] args)
    	{
    	        javax.swing.SwingUtilities.invokeLater(new Runnable()
    	        {
    	            public void run()
    		{
                 	   createAndShowGUI();
    	            }
    	        });
    	}
    }
    Last edited by Eranga; 01-17-2012 at 06:04 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Help making simple GUI prettier

    Mainly you have some issues with choosing the layouts. You better go through the following.

    Using Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    I like to move with Swing rather AWT. Look and feel of Swing is much better and easy to control.

    Main frame size is not suitable to display all the controls you have added.


    Java Code:
    MainFrame.setSize(new Dimension(750, 300));  // size of window

  3. #3
    switch's Avatar
    switch is offline Member
    Join Date
    Jan 2012
    Location
    U.S.A.
    Posts
    19
    Rep Power
    0

    Default Re: Help making simple GUI prettier

    I like using GridbagLayout the best once I understood it. I've used BorderLayout and FlowLayout, but Gridbag seems to be the the best layout for positioning components. Of course thats just me, but I just thought I'd express my two cents.

    switch

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help making simple GUI prettier

    I agree with Eranga: please read or re-read the layout managers and learn to nest JPanels each using it's own layout manager and to experiment. I disagree with switch -- no one layout should be your "favorite" but instead learn them all well. For instance, GridBagLayout would be a bad fit for your current simple GUI and would likely be over-kill.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Help making simple GUI prettier

    Quote Originally Posted by switch View Post
    I like using GridbagLayout the best once I understood it. I've used BorderLayout and FlowLayout, but Gridbag seems to be the the best layout for positioning components. Of course thats just me, but I just thought I'd express my two cents.

    switch
    That's the way mate. Do some experiment while doing your main stuff. Try to write some lines of code on different things each day. You can lean a lot yourself and it'll really helpful to make your day to day projects improves in many ways.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  2. need help with a simple game im making
    By frallan in forum AWT / Swing
    Replies: 2
    Last Post: 11-30-2009, 10:36 AM
  3. making simple java environment
    By daanlaan in forum New To Java
    Replies: 0
    Last Post: 07-07-2009, 12:22 AM
  4. Making simple hiscore over internet
    By jurka in forum Networking
    Replies: 3
    Last Post: 01-21-2009, 03:23 PM
  5. Replies: 1
    Last Post: 10-02-2008, 03:06 AM

Posting Permissions

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