Results 1 to 2 of 2
  1. #1
    philgrek is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default centering/sizing fields on a panel

    I am having a hard time getting my text field and password fields to size correctly and to appear in the middle of the screen. Any help?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LoginScreen extends JFrame
    {
    	public static void main(String[] args) {
    		new LoginScreen();
    	}
    	 
    	 public LoginScreen() {
    	 	 
    	    super("Log-In");
    	    this.setDefaultCloseOperation(super.EXIT_ON_CLOSE);
    	
    	    Container content = getContentPane();
    	    content.setPreferredSize(new Dimension(400,200));
    	    content.setBackground(Color.black);
    	    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    	    content.add(Box.createRigidArea(new Dimension(0,100)));
    	    
    	    JFormattedTextField user = new JFormattedTextField();
    	    JPasswordField pword = new JPasswordField();
    	    	    	
    	    content.add(user, BorderLayout.PAGE_START);
    	    user.setSize(5,2);
    	   
    	    content.add(Box.createRigidArea(new Dimension(0,30)));
    	    content.add(pword, BorderLayout.PAGE_START);
    	    pword.setSize(5,2);
    	    	   
    	    pack();	
    	    setVisible(true);
    	}
    	
    	
    	        	
    }

  2. #2
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: centering/sizing fields on a panel

    Hi,

    first I would put this after setDefaultCloseOperation():
    Java Code:
    this.setLocationRelativeTo(null);
    so your JFrame will be placed in the middle of you screen.
    And for the textfield itself I would use the GridLayout(int rows,int columns);
    In that way you don't have to place the size. All component will have the same size and would be allign.

Similar Threads

  1. JTable sizing issues
    By Timmins in forum AWT / Swing
    Replies: 6
    Last Post: 04-02-2011, 03:40 AM
  2. Panel sizing
    By sjs in forum AWT / Swing
    Replies: 5
    Last Post: 10-11-2010, 04:13 AM
  3. component centering
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-25-2010, 12:48 AM
  4. Trouble with Buffer Sizing
    By Jeff in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 02:43 PM
  5. Help needed with sizing components
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 05:40 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
  •