GridBagLayout 显示这些组件彼此分开
GridBagLayout showing these components apart from each other
i want it to be like this我正在练习 [GridBagLayout]
我已经在网上搜索了但我没有找到我的问题的答案然后我来这里寻求帮助,他们为什么要去彼此分开
package swing;
import javax.swing.*;
import java.awt.*;
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.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridheight = 1;
gbc.gridwidth = 5;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JLabel stProfile = new JLabel("Student Profile");
c.add(stProfile, gbc);
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.gridy = 1;
c.add(j1, gbc);
}
public static void main (String[] agrs) {
StudentProfile sp = new StudentProfile();
sp.setVisible(true);
}
}
enter image description here
我建议您尝试使用 .insets 来设置顶部、底部、左侧和右侧边距。
请注意,您需要初始化 new Insets() 对象,该对象将采用预测边距的参数。
编辑:
我再次浏览了一遍代码,实际上,您不必使用 insets 来实现您想要实现的目标。
public class Fixed_StudentProfile extends JFrame {
private JLabel stProfile_label;
private JLabel stName_label;
private JLabel fatherName_label;
public Fixed_StudentProfile() {
super("Student profile");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 400);
setVisible(true);
createView();
}
public void createView() {
stProfile_label = new JLabel("Student profile: ");
stName_label = new JLabel("Name: ");
fatherName_label = new JLabel("Father: ");
JPanel panelMain = new JPanel();
getContentPane().add(panelMain,BorderLayout.WEST);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
panelForm.add(stProfile_label, c);
c.gridy++; // move down the line
panelForm.add(stName_label, c);
c.gridy++; // move down the line
panelForm.add(fatherName_label, c);
}
public static void main(String[] args) {
Fixed_StudentProfile s = new Fixed_StudentProfile();
}
1. 将您的组件声明为全局变量 是一个好习惯比本地的。
2. 创建 JPanel,将其添加到 ContentPane 并 将 BorderLayout 设置为 WEST!
(您要将组件(标签)添加到 JPanel 而不是 JFrame)。
3. 其他都很简单,创建GridBagConstraints对象.
4. 谨防 将 ANCHOR 设置为 FIRST_LINE_START。
5. 基本上就是这样,现在你只需要增加你的网格轴 因为您希望您的组件以这种方式对齐。
除了我上面提到的插图你可以尝试添加这个
创建 GridBagConstraints 对象后的代码行。
c.insets = new Insets(5,5,5,5);
这将在您的 JLabel 区域周围添加漂亮的间距,您将有效地避免它看起来太密集。
这东西是有重量的。在放置最后一个元素之前使 weighty=1
正确。并使用 Insets
在组件之间创建间隙。请参阅下面代码中的注释。请记住使用 gbc.fill
,这会使组件沿您指定的方向拉伸(HORIZONTAL
、VERTICAL
、BOTH
、NONE
)。此外,您对 gbc
和 gbc1
对象造成了很多混乱。尝试将您的代码重构为更小的 classes/methods - 阅读 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);
});
}
}
如果有帮助,请采纳此答案。 :)
i want it to be like this我正在练习 [GridBagLayout]
我已经在网上搜索了但我没有找到我的问题的答案然后我来这里寻求帮助,他们为什么要去彼此分开
package swing;
import javax.swing.*;
import java.awt.*;
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.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridheight = 1;
gbc.gridwidth = 5;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JLabel stProfile = new JLabel("Student Profile");
c.add(stProfile, gbc);
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.gridy = 1;
c.add(j1, gbc);
}
public static void main (String[] agrs) {
StudentProfile sp = new StudentProfile();
sp.setVisible(true);
}
}
enter image description here
我建议您尝试使用 .insets 来设置顶部、底部、左侧和右侧边距。 请注意,您需要初始化 new Insets() 对象,该对象将采用预测边距的参数。
编辑:
我再次浏览了一遍代码,实际上,您不必使用 insets 来实现您想要实现的目标。
public class Fixed_StudentProfile extends JFrame {
private JLabel stProfile_label;
private JLabel stName_label;
private JLabel fatherName_label;
public Fixed_StudentProfile() {
super("Student profile");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 400);
setVisible(true);
createView();
}
public void createView() {
stProfile_label = new JLabel("Student profile: ");
stName_label = new JLabel("Name: ");
fatherName_label = new JLabel("Father: ");
JPanel panelMain = new JPanel();
getContentPane().add(panelMain,BorderLayout.WEST);
JPanel panelForm = new JPanel(new GridBagLayout());
panelMain.add(panelForm, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
panelForm.add(stProfile_label, c);
c.gridy++; // move down the line
panelForm.add(stName_label, c);
c.gridy++; // move down the line
panelForm.add(fatherName_label, c);
}
public static void main(String[] args) {
Fixed_StudentProfile s = new Fixed_StudentProfile();
}
1. 将您的组件声明为全局变量 是一个好习惯比本地的。
2. 创建 JPanel,将其添加到 ContentPane 并 将 BorderLayout 设置为 WEST! (您要将组件(标签)添加到 JPanel 而不是 JFrame)。
3. 其他都很简单,创建GridBagConstraints对象.
4. 谨防 将 ANCHOR 设置为 FIRST_LINE_START。
5. 基本上就是这样,现在你只需要增加你的网格轴 因为您希望您的组件以这种方式对齐。
除了我上面提到的插图你可以尝试添加这个 创建 GridBagConstraints 对象后的代码行。
c.insets = new Insets(5,5,5,5);
这将在您的 JLabel 区域周围添加漂亮的间距,您将有效地避免它看起来太密集。
这东西是有重量的。在放置最后一个元素之前使 weighty=1
正确。并使用 Insets
在组件之间创建间隙。请参阅下面代码中的注释。请记住使用 gbc.fill
,这会使组件沿您指定的方向拉伸(HORIZONTAL
、VERTICAL
、BOTH
、NONE
)。此外,您对 gbc
和 gbc1
对象造成了很多混乱。尝试将您的代码重构为更小的 classes/methods - 阅读 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);
});
}
}
如果有帮助,请采纳此答案。 :)