JTextField Fixed Height

unleashed picture unleashed · Apr 1, 2011 · Viewed 62.8k times · Source

How do I get the JTextField to have a fixed height when the Frame is maximized? I want it to look sort of similar to the Skype application on Ubuntu.

private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){


    setSize(200,200);
    setLayout(new GridLayout(4,4));
    setBackground(new Color(85,153,187));
    setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
    username = new JTextField();
    password = new JPasswordField();
    usernamelabel= new JLabel("Username");
    passwordlabel= new JLabel("Password");
    username.setBounds(5, 5, 100, 100);
    username.setPreferredSize(new Dimension(80,20));
    password.setPreferredSize(new Dimension(80,20));
    add(usernamelabel);
    add(username);
    add(passwordlabel);
    add(password);

Answer

jzd picture jzd · Apr 1, 2011

Don't use a layout other than GridLayout or put the text field in another panel that has a FlowLayout that sits inside of your GridLayout.