【问题标题】:GridBagLayout showing these components apart from each otherGridBagLayout 显示这些组件彼此分开
【发布时间】:2017-12-17 08:08:36
【问题描述】:

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

【问题讨论】:

标签: java swing gridbaglayout


【解决方案1】:

事情是重量。在放置最后一个元素之前制作weighty=1。并使用Insets 在组件之间创建间隙。请参阅下面代码中的 cmets。请记住使用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);
}

结果:

另外,看看这段代码,它是你的修改版本。

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

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

【讨论】:

  • 实际上我接受了您以前的代码,当您给我解决方案时我尝试接受它,但由于某种原因我无法接受您的解决方案,感谢您的帮助
【解决方案2】:

我可以建议您尝试使用 .insets 来设置顶部、底部、左侧和右侧边距。 请注意,您需要初始化 new 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 设置为 WES​​T! (您要将组件(标签)添加到 JPanel 而不是 JFrame)。

3. 其他都比较简单,创建 GridBagConstraints 对象

4.注意将 ANCHOR 设置为 FIRST_LINE_START

5.基本上就是这样,现在您只需要增加网格轴,因为您希望组件以这种方式对齐.


除了我上面提到的插图之外,您还可以尝试添加这个 创建 GridBagConstraints 对象后的代码行。

c.insets = new Insets(5,5,5,5);

这将在您的 JLabel 区域周围添加很好的间距,您将有效地避免它看起来如此密集。

【讨论】:

  • 这并不能完全回答问题。把它放在他的例子中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
相关资源
最近更新 更多