gpt4 book ai didi

java - 将组件添加到我的 GridBagLayout 会将其他组件移到最右侧

转载 作者:行者123 更新时间:2023-12-01 22:18:45 33 4
gpt4 key购买 nike

当我尝试布置我的 JPanel 时,我看到了一些非常奇怪的结果。在添加最后一个 JTextField subline 之前,一切正常:

correct layout before adding last JTextField

当我添加它时,其上方的 JTextField 会向右移动,以便它从 subline 结束处开始:

incorrect layout after adding last JTextField

这是创建此布局的代码:

public class Opspanel extends JPanel{
private static final long serialVersionUID = -6393281054430179953L;
public Opspanel() {
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();

constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Vendor ID"), constraints);

constraints.gridx = 1;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.LINE_START;
JTextField vendorid = new JTextField();
vendorid.setPreferredSize(new Dimension(100,20));
add(vendorid, constraints);

constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Email Date"), constraints);

constraints.gridx = 1;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.LINE_START;
JTextField emaildate = new JTextField();
emaildate.setPreferredSize(new Dimension(100,20));
add(emaildate, constraints);

constraints.gridx = 2;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Email Time"), constraints);

constraints.gridx = 3;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.LINE_START;
JTextField emailtime = new JTextField();
emailtime.setPreferredSize(new Dimension(100,20));
add(emailtime, constraints);

constraints.gridx = 0;
constraints.gridy = 2;
constraints.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Subject Line"), constraints);

constraints.gridx = 1;
constraints.gridy = 2;
constraints.anchor = GridBagConstraints.LINE_START;
JTextField subline = new JTextField();
subline.setPreferredSize(new Dimension(500, 20));
add(subline, constraints);

}
}

我该如何解决这个问题?

最佳答案

根据the docs for GridBagLayout ,

Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area.

在您的代码中,网格有 3 行和 4 列。您告诉每个组件恰好占据一个单元格,位置由 gridxgridy 指定。

您不喜欢其中一些“停止”或“开始”,这一事实表明您确实希望其中一些占据多个单元格。

你很幸运! GridBagLayout 可以做到这一点。您正在寻找GridBagLayout.gridwidth :

Specifies the number of cells in a row for the component's display area.

Use REMAINDER to specify that the component's display area will be from gridx to the last cell in the row. Use RELATIVE to specify that the component's display area will be from gridx to the next to the last one in its row.

gridwidth should be non-negative and the default value is 1.

<小时/>

在您的示例中,您将“主题行”文本字段放在第二列中 (gridx = 1)。由于您将 gridwidth 保留为其默认值 1,因此该文本字段无法与第三列或第四列重叠。因此,如果它增长,整个列也必须随之增长。

您可以通过让它覆盖多个网格单元来让它填充整个水平空间。 (如果您熟悉 Microsoft Excel 或 Microsoft Word 或类似程序中的表格,这就像“合并单元格”选项。)

要使其覆盖 3 列,从 gridx = 1 开始:

constraints.gridx = 1;
constraints.gridwidth = 3;

或者,使其覆盖从 gridx = 1 列到右侧的所有单元格:

constraints.gridx = 1;
constraints.gridwidth = GridBagLayout.REMAINDER;

在使用它添加另一个仅覆盖一列的组件之前,请不要忘记将 gridwidth 重置为 1!

关于java - 将组件添加到我的 GridBagLayout 会将其他组件移到最右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415052/

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