除第一个单元格外的x单元格不会占用多余的空间。我首先专注于尝试解决名称部分,现在可以使用,但是生日部分中的3行部分 应该看起来像this
在这里我无法确定逻辑错误,因为我遵循以下标准:将填充设置为水平,将权重设置为1,设置网格宽度并更新x和y。
panelMain = new JPanel();
getContentPane().add(panelMain);
panelMain.setBackground(Color.WHITE);
panelForm = new JPanel(new GridBagLayout());
panelForm.setBackground(formColor);
panelMain.add(panelForm);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,20,0,20);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
lblName = new JLabel("Name");
panelForm.add(lblName, c);
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 0.5;
txfFirst = new JTextField("First");
panelForm.add(txfFirst, c);
c.gridx = 1;
c.gridwidth = 1;
c.weightx = 0.5;
txfLast = new JTextField("Last");
panelForm.add(txfLast, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
c.weightx = 1;
lblEmail = new JLabel("Your email address");
panelForm.add(lblEmail, c);
c.gridy = 3;
txfEmail = new JTextField();
panelForm.add(txfEmail, c);
c.gridy = 4;
lblPw = new JLabel("Create a password");
panelForm.add(lblPw, c);
c.gridy = 5;
pwfPw = new JPasswordField();
panelForm.add(pwfPw, c);
c.gridy = 6;
lblConf = new JLabel("Confirm your password");
panelForm.add(lblConf, c);
c.gridy = 7;
pwfConf = new JPasswordField();
panelForm.add(pwfConf, c);
c.gridy = 8;
lblBday = new JLabel("Birthday");
panelForm.add(lblBday, c);
c.gridy = 9;
c.gridwidth = 1;
c.gridx = 0;
cmbMonth = new JComboBox(months);
panelForm.add(cmbMonth, c);
c.gridx = 1;
c.gridwidth = 1;
c.weightx = 1;
txfDay = new JTextField("Day");
panelForm.add(txfDay, c);
c.gridx = 2;
c.gridwidth = 1;
c.weightx = 1;
txfYear = new JTextField("Year");
panelForm.add(txfYear, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 10;
lblGend = new JLabel("Gender");
panelForm.add(lblGend, c);
c.gridy = 11;
cmbGend = new JComboBox();
panelForm.add(cmbGend,c );
c.gridy = 12;
lblPhone = new JLabel("Mobile phone");
panelForm.add(lblPhone, c);
c.gridy = 13;
txfPhone = new JTextField();
panelForm.add(txfPhone, c);
c.gridy = 14;
lblLoc = new JLabel("Location");
panelForm.add(lblLoc, c);
c.gridy = 15;
cmbLoc = new JComboBox();
panelForm.add(cmbLoc, c);
c.gridy = 16;
c.gridx = 2;
btnNext = new JButton("Next Step");
panelForm.add(btnNext, c);