To respond to a layout using standard layout managers (except for the GridBag), plan layouts as follows:
BorderLayout
NORTH=BorderLayout
WEST=GridLayout(0,1)
Label "UserName")
Label "Password")
CENTER=GridLayout(0,1)
Text Field
Password Field
The code would look like
JPanel outer = new JPanel(new BorderLayout());
JPanel top = new JPanel(new BorderLayout());
JPanel labels = new JPanel(new GridLayout(0,1,3,3));
JPanel fields = new JPanel(new GridLayout(0,1,3,3));
outer.add(top, BorderLayout.NORTH);
top.add(labels, BorderLayout.WEST);
top.add(fields, BorderLayout.CENTER);
labels.add(new JLabel("Username"));
labels.add(new JLabel("Password"));
fields.add(new JTextField());
fields.add(new JPasswordField());
. http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/ ( ) , , .
GridBag - ; , , . , :
Insets i = new Insets(0,0,0,0);
p.setLayout(new GridBagLayout());
p.add(new JLabel("Username"),
new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, i, 0, 0));
p.add(new JLabel("Password"),
new GridBagConstraints(0, 1, 1, 1, 0, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, i, 0, 0));
p.add(new JTextField(),
new GridBagConstraints(1, 0, 1, 1, 1, 0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, i, 0, 0));
p.add(new JPasswordField(),
new GridBagConstraints(1, 1, 1, 1, 1, 1,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, i, 0, 0));
, "" ...