事情是重量。在放置最后一个元素之前制作weighty=1。并使用Insets 在组件之间创建间隙。请参阅下面代码中的 cmets。请记住使用gbc.fill,它使组件在您指定的方向上拉伸(HORIZONTAL、VERTICAL、BOTH、NONE)。此外,您的gbc 和gbc1 对象也很麻烦。尝试将您的代码重构为更小的类/方法 - 阅读 Single Responsibility Principle。一般来说,在变量名中使用数字并不是一个好主意。
SSCCE
public class StudentProfile extends JFrame {
public StudentProfile() {
super("Student Profile");
setSize(400, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 10, 0); //<---1 this to make a bottom gap after first label
gbc.weightx = 1.0;
//gbc.weighty = 1.0; <---2 take this from here
gbc.gridheight = 1;
gbc.gridwidth = 5;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JLabel stProfile = new JLabel("Student Profile");
c.add(stProfile, gbc);
gbc.insets = new Insets(0, 0, 0, 0); //<---1 reset insets
JPanel j1 = new JPanel();
j1.setLayout(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
JLabel stName = new JLabel("Student Name", SwingConstants.LEFT);
j1.add(stName, gbc1);
gbc1.gridy = 1;
JLabel fName = new JLabel("Father Name", SwingConstants.LEFT);
j1.add(fName, gbc1);
gbc.weighty = 1.0; //<---2 here
gbc.gridy = 1;
c.add(j1, gbc);
}
public static void main (String[] agrs) {
StudentProfile sp = new StudentProfile();
sp.setVisible(true);
}
结果:
另外,看看这段代码,它是你的修改版本。
public class StudentProfile extends JFrame {
private Container container;
private JPanel studentProfileInfoPanel;
public StudentProfile(String title) {
super(title);
}
public void createStudentProfile() {
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.container = this.getContentPane();
this.container.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 10, 0);
gbc.weightx = 1.0;
gbc.gridx=0;
gbc.gridy=0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel studentProfileLabel = new JLabel("Student Profile");
this.container.add(studentProfileLabel, gbc);
this.createStudentProfileInfoPanel();
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weighty = 1.0;
gbc.gridy++;
//gbc.fill=GridBagConstraints.NONE; //<--- try using this, see what happens
this.container.add(studentProfileInfoPanel, gbc);
}
private void createStudentProfileInfoPanel() {
this.studentProfileInfoPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10,0,0,10);
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel studentNameLabel = new JLabel("Student Name");
this.studentProfileInfoPanel.add(studentNameLabel, gbc);
gbc.gridx++;
this.studentProfileInfoPanel.add(new JLabel ("Adam Smith Jr"), gbc);
gbc.gridx=0;
gbc.gridy++;
JLabel studentFatherNameLabel = new JLabel("Father Name");
this.studentProfileInfoPanel.add(studentFatherNameLabel, gbc);
gbc.weighty = 1;
gbc.gridx++;
this.studentProfileInfoPanel.add(new JLabel("Adam Smith"), gbc);
}
public static void main(String[] agrs) {
StudentProfile studentProfile = new StudentProfile("Student Profile");
studentProfile.createStudentProfile();
SwingUtilities.invokeLater(() -> {
studentProfile.setVisible(true);
});
}
}
如果这有帮助,请接受此答案。 :)