【问题标题】:GridBagLayout inside a JPanel and JPanel inside GridBagLayoutJPanel 内的 GridBagLayout 和 GridBagLayout 内的 JPanel
【发布时间】:2014-04-03 13:33:27
【问题描述】:

我有两个 Java 类:

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

我的代码如下:


第一类


class2 one = new class2();
class2 two = new class2();

this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(6,2,6,40);

gbc.gridx = 0;
this.add(new JLabel("Name"), gbc);
gbc.gridx = 1;
this.add(new JLabel("Surname"), gbc);
gbc.gridx = 2;
this.add(new JLabel("Age"), gbc);

gbc.gridx = 0;

gbc.gridy = 1;
this.add(one, gbc);     

gbc.gridy = 2;
this.add(two, gbc);

Class2 扩展了 JPanel


this.setLayout(new GridBagLayout());
GridBagConstraints gridBagCons = new GridBagConstraints();

gridBagCons.insets = new Insets(6,2,6,4);

gridBagCons.gridx = 0;
this.add(new JTextField(10), gridBagCons);
gridBagCons.gridx = 1;
this.add(new JTextField(10), gridBagCons);
gridBagCons.gridx = 2;
this.add(new JTextField(10), gridBagCons);

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

请你帮我看看。

【问题讨论】:

  • 在您发布 SSCCE / MCVE / MCTRE、简短、可运行、可编译的情况下,这可能是一个关于嵌套布局的好问题
  • 你能画出或解释一下你想要的样子吗?
  • @mKorbel 我已经稍微修改了问题,所以更清楚了。我现在需要将 class1 的标题与 class2 的文本字段对齐

标签: java swing gridbaglayout


【解决方案1】:

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

gbc.gridWidth = 3;

在添加Class2 面板之前。

要对齐文本字段,请将

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

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

【讨论】:

  • 如果他确实想要的话,这不会将文本字段与标题对齐。
  • 这个答案帮助我组织得很好。但是@user1803551 是正确的,它没有对齐标题下方的文本字段。你能帮我把它们放在类下面吗?
  • 编辑了答案。手动设置插图时要小心。考虑到 Class1 的面板有一个 GridBagLayout,Class2 的每个面板都有一个。
【解决方案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);
    }
}

【讨论】:

  • 现在越来越乱了。我需要找到另一种方法来不在 class2 上设置 GidBagLayount 并且只在 class1 上设置。有什么想法吗?
  • 你最终想要得到什么?动态添加行?你必须使用特定的布局?您可以使用GridLayout 获得类似的结果。
  • 是的,所以我想动态添加行,然后 class2 中的每个文本字段都应该在其各自的标题下方
  • 我编辑了我的答案,以GridLayout 为例。当然还有其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2014-10-28
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多