gpt4 book ai didi

java - GridBagLayout 对齐不应该的列

转载 作者:行者123 更新时间:2023-12-02 06:01:15 25 4
gpt4 key购买 nike

很抱歉又问了一个“我的代码出了什么问题”的问题,但我一直在与这个 GridBagOfHurtLayout 作斗争,我不明白可能会发生什么。

我想制作一个具有如下布局的用户界面:

What it should be

绘画程序崩溃了,我得到的只是一张缩小的屏幕截图,对于颗粒感表示抱歉。所以在我看来,我想要一个 4 列的 GridBagLayout,顶部两个 Pane 各跨 2 列,文本字段和大 Pane 跨 3 列,搜索按钮和左下 Pane 跨 1 列(水平)。这是我这样做的代码:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(1080, 720));

this.setLayout(new GridBagLayout());
GridBagConstraints constraints;

projectLabel = new JLabel("Projects");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.LINE_START;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.weightx = 0.25;
constraints.weighty = 0;
constraints.insets = new Insets(4,4,4,4);
this.add(projectLabel);

projectScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 0.5;
constraints.insets = new Insets(4,4,4,4);
projectScrollPane.setPreferredSize(new Dimension(540, 240));
this.add(projectScrollPane,constraints);

fieldLabel = new JLabel("Fields");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.LINE_START;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 0;
constraints.insets = new Insets(4,4,4,4);
this.add(fieldLabel);

fieldScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 2;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 0.5;
constraints.insets = new Insets(4,4,4,4);
fieldScrollPane.setPreferredSize(new Dimension(540, 240));
this.add(fieldScrollPane,constraints);

searchTextField = new JTextField();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.weightx = 2.0;
constraints.weighty = 0.0;
constraints.insets = new Insets(4,4,4,4);
searchTextField.setPreferredSize(new Dimension(1000,30));
this.add(searchTextField,constraints);

searchButton = new JButton("Search");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.gridx = 3;
constraints.gridy = 2;
constraints.weightx = 0.0;
constraints.weighty = 0.0;
constraints.insets = new Insets(4,4,4,4);
searchButton.setSize(new Dimension(80,30));
this.add(searchButton,constraints);

imageScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 3;
constraints.weightx = 0.25;
constraints.weighty = 1.0;
constraints.insets = new Insets(4,4,4,4);
imageScrollPane.setPreferredSize(new Dimension(360,810));
this.add(imageScrollPane,constraints);

imagePane = new JEditorPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 3;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.insets = new Insets(4,4,4,4);
imagePane.setPreferredSize(new Dimension(720,810));
this.add(imagePane,constraints);

this.pack();

标签不在原始布局中。每个 ScrollPanes 最终都会包含一个 JList,但现在我只是试图让空布局看起来正确,但事实并非如此。我目前得到的是这个。

What I actually get

无法说服文本字段占用 3 列,或者第一个滚动 Pane 可能占用 3 列。我已尝试以我能想到的各种方式更改它,但没有任何帮助。我可能会放弃这个并使用 BoxLayout 进行嵌套面板,但我想知道为什么这不起作用,因为我认为我做的一切都是正确的。有什么见解吗?提前致谢。

最佳答案

Swing 在这里会感到困惑,因为它实际上没有引用列应该在的位置。

这种情况下的最佳解决方案是创建至少一行包含所有列的行,并在这些列上定义weightx。

此外,JEditorPane 在 gridBagLayout 中不能很好地工作。您应该使用 JScrollPane(或其他东西)并将 JEditorPane 放入此 Pane 中。

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(1080, 720));

this.setLayout(new GridBagLayout());
GridBagConstraints constraints;

JLabel a = new JLabel();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx=0.25;
constraints.insets = new Insets(4,4,4,4);
this.add(a,constraints);

JLabel b = new JLabel();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 1;
constraints.gridy = 0;
constraints.weightx=0.25;
this.add(b,constraints);

JLabel c = new JLabel();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.weightx=0.5;
constraints.insets = new Insets(4,4,4,4);
this.add(c,constraints);

JLabel d = new JLabel();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 3;
constraints.gridy = 0;
constraints.weightx=0;
this.add(d,constraints);

JLabel p1 = new JLabel("Project - 1");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth=2;
constraints.insets = new Insets(4,4,4,4);
this.add(p1,constraints);

JLabel p2 = new JLabel("Project - 2");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth=2;
constraints.insets = new Insets(4,4,4,4);
this.add(p2,constraints);


JScrollPane projectScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.weighty=1;
constraints.insets = new Insets(4,4,4,4);
this.add(projectScrollPane,constraints);

JScrollPane fieldScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 2;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.insets = new Insets(4,4,4,4);
fieldScrollPane.setPreferredSize(new Dimension(540, 240));
this.add(fieldScrollPane,constraints);

JTextField searchTextField = new JTextField();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.insets = new Insets(4,4,4,4);
searchTextField.setPreferredSize(new Dimension(1000,30));
this.add(searchTextField,constraints);

JButton searchButton = new JButton("Search");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.LINE_END;
constraints.gridx = 3;
constraints.gridy = 2;
constraints.insets = new Insets(4,4,4,4);
searchButton.setSize(new Dimension(80,30));
this.add(searchButton,constraints);

JScrollPane imageScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 3;
constraints.insets = new Insets(4,4,4,4);
constraints.weighty=1;
this.add(imageScrollPane,constraints);

JScrollPane imagePane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 3;
constraints.weighty=1;
constraints.insets = new Insets(4,4,4,4);
this.add(imagePane,constraints);

this.pack();

关于java - GridBagLayout 对齐不应该的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22665272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com