Java GridBagConstraints
Eu tenho 5 componentes em um JPanel. Tudo está parecendo liso com os primeiros 4 componentes que eu adicionei. No entanto, quando tento adicionar um quinto componente ao JPanel, o espaçamento entre os componentes muda por qualquer motivo!
Sem o quinto componente:
First Name: [..............]
Last Name: [..............]
Com:
First Name:---------------- [.............]
Last Name:----------------- [.............]
What is your favorite sport:
Finja que os traços acima entre rótulo e campo de texto são espaço
O espaçamento entre os rótulos e o campo de texto mudam! Aqui está o meu código, por favor me ajude!
public static void main(String[] args)
{
JFrame frame = new JFrame("Survey");
frame.setSize(800, 600);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel(new GridBagLayout());
//LINE.START = Top Left Corner
frame.add(p1);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 10, 10);
JLabel lblFirstN = new JLabel("First Name:");
JLabel lblLastN = new JLabel("Last Name:");
JLabel lblFavSport = new JLabel("What is your favorite sport:");
JTextField txtFirstN = new JTextField();
txtFirstN.setPreferredSize(new Dimension(100, 20));
JTextField txtLastN = new JTextField();
txtLastN.setPreferredSize(new Dimension(100, 20));
gbc.gridx = 0;
gbc.gridy = 0;
p1.add(lblFirstN, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
p1.add(txtFirstN, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
p1.add(lblLastN, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
p1.add(txtLastN, gbc);
//this block of code is what is screwing me
gbc.gridx = 0;
gbc.gridy = 2;
p1.add(lblFavSport, gbc);
}