GridBagLayout显示这些组件彼此分开

时间:2017-07-12 18:48:16

标签: java swing gridbaglayout

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

2 个答案:

答案 0 :(得分:0)

我建议您尝试使用 .insets 来设置上边距,下边距,左边距和右边距。 请注意,您需要初始化新的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区域周围添加漂亮的间距,您将有效地避免它看起来如此密集。

答案 1 :(得分:0)

事情是权重。在放置最后一个元素之前立即weighty=1。并使用Insets在组件之间创建间隙。请参阅以下代码中的注释。请务必使用gbc.fill,这会使组件沿您指定的方向伸展(HORIZONTALVERTICALBOTHNONE)。此外,您对gbcgbc1对象进行了大量修改。尝试将代码重构为较小的类/方法 - 阅读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);
}

结果:

Result layout

另外,请查看此代码,这是您的修改版本。

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);
        });
    }
}

如果这有帮助,请接受此答案。 :)