Java JPanel中的GridBagLayout和GridBagLayout中的JPanel

Java JPanel中的GridBagLayout和GridBagLayout中的JPanel,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我有两个Java类: class1调用class2的多个实例 class2扩展了JPanel 所以我想在class1中设置GridBagLayout,这样我就可以将class2的实例添加到列表中 我的代码如下: 第一类 Class2扩展了JPanel 因此,它不起作用,它显示标题:姓名、姓氏和年龄,正如我所希望的,以及从class1的GridBagLayout的第一列中导入的其他两个面板。 我想将标题与从class2导入的文本字段对齐 请帮我看看。您需要在gridbagstraint

我有两个Java类:

  • class1调用class2的多个实例
  • class2扩展了JPanel 所以我想在class1中设置
    GridBagLayout
    ,这样我就可以将class2的实例添加到列表中
我的代码如下:


第一类



Class2扩展了JPanel


因此,它不起作用,它显示标题:姓名、姓氏和年龄,正如我所希望的,以及从class1的GridBagLayout的第一列中导入的其他两个面板。 我想将标题与从class2导入的文本字段对齐


请帮我看看。

您需要在
gridbagstraints
上设置
gridwidth
。您的示例创建了一个3x3网格。标题在第一行。在第二行,第一个位置包含整个
Class2
面板,第2列和第3列没有任何内容。第三排也一样

gbc.gridWidth=3

在添加
Class2
面板之前

要对齐文本字段,请将

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;

对于“标题”的每个标签和类别2的每个文本字段。手动设置插图将无法正确调整大小。

您可以手动调整插图

gbc.insets = new Insets(6,2,6,90);
gbc.gridx = 0;
this.add(new JLabel("Name"), gbc);
gbc.insets = new Insets(6,0,6,-20);
gbc.gridx = 1;
this.add(new JLabel("Surname"), gbc);
gbc.insets = new Insets(6,0,6,0);
gbc.gridx = 2;
this.add(new JLabel("Age"), gbc);

gbc.gridx = 0;
gbc.gridwidth = 3;

gbc.gridy = 1;
this.add(one, gbc);     
gbc.gridy = 2;
this.add(two, gbc);
尽管您最好不要使用第二个类,并根据您的实现以另一种方式添加文本字段


编辑:这里有一种使用
GridLayout动态添加字段的方法

public class AddingFields {

    static GridLayout gl = new GridLayout(2, 3, 20, 10);
    static JFrame frame = new JFrame();

    public static void main(String[] args) {

        new AddingFields();
        createAdderFrame();
    }

    AddingFields() {

        frame.setLayout(gl);

        frame.add(new JLabel("Name"));
        frame.add(new JLabel("Surname"));
        frame.add(new JLabel("Age"));

        frame.add(new JTextField(10));
        frame.add(new JTextField(10));
        frame.add(new JTextField(10));

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static void createAdderFrame() {

        JFrame adder = new JFrame();
        JButton addButton = new JButton("Add row");
        adder.add(addButton);
        addButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                gl.setRows(gl.getRows() + 1);
                frame.add(new JTextField(10));
                frame.add(new JTextField(10));
                frame.add(new JTextField(10));
                frame.pack();
            }
        });
        adder.pack();
        adder.setVisible(true);
    }
}

如果你发布了一个SSCCE/MCVE/MCTRE,short,runnable,compileable,那么这可能是一个关于嵌套布局的好问题。你能画出或解释你想要它的样子吗?@mKorbel我对这个问题做了一点修改,所以它更清晰。我现在需要将class1的标题与Class2的文本字段对齐。如果他真的希望这样做,则不会将文本字段与标题对齐。这个答案帮助我组织得很好。然而@user1803551是正确的,它没有对齐标题下面的文本字段。你能帮我把它们排在课后吗?编辑答案。手动设置插图时要小心。考虑到1类的面板有一个GridBagLayout,2类的每个面板有一个GridBagLayout,现在变得很混乱。我需要找到另一种方法,不在类2上设置GidBagLayount,而只在类1上设置GidBagLayout。有什么想法吗?你最终想得到什么?动态添加行?您必须使用特定的布局吗?您可以使用
GridLayout
获得类似的结果。是的,因此我想动态添加行,然后class2中的每个文本字段都应该位于其各自标题的下方。我编辑了我的答案,以给出一个使用
GridLayout
的示例。当然还有其他方法。
gbc.insets = new Insets(6,2,6,90);
gbc.gridx = 0;
this.add(new JLabel("Name"), gbc);
gbc.insets = new Insets(6,0,6,-20);
gbc.gridx = 1;
this.add(new JLabel("Surname"), gbc);
gbc.insets = new Insets(6,0,6,0);
gbc.gridx = 2;
this.add(new JLabel("Age"), gbc);

gbc.gridx = 0;
gbc.gridwidth = 3;

gbc.gridy = 1;
this.add(one, gbc);     
gbc.gridy = 2;
this.add(two, gbc);
public class AddingFields {

    static GridLayout gl = new GridLayout(2, 3, 20, 10);
    static JFrame frame = new JFrame();

    public static void main(String[] args) {

        new AddingFields();
        createAdderFrame();
    }

    AddingFields() {

        frame.setLayout(gl);

        frame.add(new JLabel("Name"));
        frame.add(new JLabel("Surname"));
        frame.add(new JLabel("Age"));

        frame.add(new JTextField(10));
        frame.add(new JTextField(10));
        frame.add(new JTextField(10));

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static void createAdderFrame() {

        JFrame adder = new JFrame();
        JButton addButton = new JButton("Add row");
        adder.add(addButton);
        addButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                gl.setRows(gl.getRows() + 1);
                frame.add(new JTextField(10));
                frame.add(new JTextField(10));
                frame.add(new JTextField(10));
                frame.pack();
            }
        });
        adder.pack();
        adder.setVisible(true);
    }
}