JLabel nameLabel = new JLabel("Name:"); JTextField nameField = new JTextField(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(nameLabel) .addComponent(nameField) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(nameLabel) .addComponent(nameField) );
JLabel nameLabel = new JLabel("Name:"); JTextField firstNameField = new JTextField(); JTextField lastNameField = new JTextField(); JButton submitButton = new JButton("Submit"); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(nameLabel) .addGroup(layout.createSequentialGroup() .addComponent(firstNameField) .addComponent(submitButton) .addComponent(lastNameField)) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(nameLabel) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(firstNameField) .addComponent(submitButton) .addComponent(lastNameField)) );Both of these examples use the javax.swing.GroupLayout package library.