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);
I think this solves your problem.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
JFrame failFrame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 4));
JPanel leftTextFieldPanel = new JPanel();
leftTextFieldPanel.setLayout(new FlowLayout());
JPanel rightTextFieldPanel = new JPanel();
rightTextFieldPanel.setLayout(new FlowLayout());
JTextField leftTextField = new JTextField();
leftTextField.setPreferredSize(new Dimension(150,20));
JTextField rightTextField = new JTextField();
rightTextField.setPreferredSize(new Dimension(150,20));
leftTextFieldPanel.add(leftTextField);
mainPanel.add(leftTextFieldPanel);
rightTextFieldPanel.add(rightTextField);
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
failFrame.add(mainPanel);
failFrame.setSize(600, 400);
failFrame.setLocationRelativeTo(null);
failFrame.setVisible(true);
}
}
I used nested Layouts. On the Top is your GridLayout and the textfields are in a FlowLayout which are added on the gridLayout panel. So the textfields are no longer stretched.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With