You might do like this....
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
public class PanelAdd extends JFrame {
JPanel panelLabels, panelPasswords ;
JLabel [] userLabels ;
JPasswordField [] passwordFields;
public PanelAdd() {
panelLabels = new JPanel();
panelPasswords = new JPanel();
GridLayout panelsLayout = new GridLayout(0, 1, 0, 5);
GridLayout mainLayout = new GridLayout(1, 2);
panelLabels.setLayout(panelsLayout);
panelPasswords.setLayout(panelsLayout);
setLayout(mainLayout);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setSize(350, 150);
}
public static void main(String [] args) {
PanelAdd add = new PanelAdd();
add.addControls();
add.setVisible(true);
}
private void addControls() {
userLabels = new JLabel[3];
passwordFields = new JPasswordField[3];
panelLabels.add(new JLabel("Users"));
panelPasswords.add(new JLabel("Passwords"));
for ( int i = 0 ; i < 3 ; i++) {
userLabels[i] = new JLabel("User "+i);
passwordFields[i] = new JPasswordField();
panelLabels.add(userLabels[i]);
panelPasswords.add(passwordFields[i]);
}
add(panelLabels);
JSeparator sep = new JSeparator(JSeparator.VERTICAL);
add(sep);
add(panelPasswords);
}
}
The GridLayout
objects enable you to specify a rectangular grid in
which to place the components. Each cell in the grid is the same
height as the other cells, and each width is the same width as the
other cells. Components are stretched both vertically and horizontally
to fill the cell.