【问题标题】:Why in Java awt GridBagLayout, gridwidth or gridheight span one row or one column less than specified?为什么在 Java awt GridBagLayout 中,gridwidth 或 gridheight 跨度小于指定的一行或一列?
【发布时间】:2019-06-05 13:18:44
【问题描述】:

我正在使用 GridBagLayout 编写一个简单的 awt 程序,其中我有四个使用 gridx、gridy 对角排列的按钮。当我将 gridwidth 设置为 3 时,它只跨越 2 列,并且忽略了他自己的列。 gridheight 也是一样的(跨度少 1 行)。

import java.applet.Applet;
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class Second extends Applet{
@Override
public void init()
{
    GridBagLayout bl = new GridBagLayout();
    this.setLayout(bl);
    GridBagConstraints gc = new GridBagConstraints();
    gc.gridx = 0;
    gc.gridy= 0;

    gc.gridwidth = 4;
    gc.fill = GridBagConstraints.HORIZONTAL;

    Button btemp = new Button("Button1");
    add(btemp, gc);

    gc.gridx = 1;
    gc.gridy= 1;
    gc.gridwidth = 1;
    add(new Button("Button2"), gc);

    gc.gridx = 2;
    gc.gridy= 2;
    add(new Button("Button3"), gc);

    gc.gridx = 3;
    gc.gridy= 3;
    add(new Button("Button4"), gc);
    setVisible(true);
}
}

【问题讨论】:

  • 我知道这不能回答您的问题,但您是否研究过 JavaFX 框架?在新版本的 java 中成为标准,并且可以更容易地在(XML 布局!)中编写 GUI
  • 您的代码未将网格宽度设置为 3。
  • @MeetTitan 我是老师,我们有旧的教学大纲
  • @VGR gridwidth 为 4,但它跨越 3 列,它忽略了他自己的列
  • 一个GridBagLayout有灵活的行和列;仅使用四列不会创建四个相同大小的列。 GridBagLayout 中的列(或行)不占用任何空间,除非其中包含定义其大小的组件。

标签: java awt layout-manager gridbaglayout


【解决方案1】:

问题中的原始代码呈现如下:

你希望它像这样渲染,对吧?

嗯...我通过在add(new Button("Button4"), gc); 行之后添加这些行来制作该图像。 (您添加Components 的顺序似乎不会影响结果。)


    //First, let us tell AWT to figure out the sizes of components
    //  it has so far.  If we do not do this, calls to getWidth()
    //  will return 0 sometimes!  It appears to be a race condition,
    //  but that's why this layoutContainer method exists.
    bl.layoutContainer(this);

    //Now, let us target the missing grid cell.
    gc.gridx = 0;
    gc.gridy = 1;

    //Let us put something in there, so the whole column has a
    //  non-zero width.  Using the width of an existing button
    //  is better than just picking some number of pixels.
    add(Box.createHorizontalStrut(this.getComponent(2).getWidth()),gc);

    //Let us adjust the applet window size (optional)
    this.setSize(this.getComponent(2).getWidth() * 4, 200);

您是否愿意在网格中的那个位置粘贴一个不可见的组件,或者您需要以不同的方式解决这个问题?

【讨论】:

  • 我看不出有充分理由在字符串中使用空格来布局 GUI 中的组件。海报还说他是一名教练。我不建议教学生解决琐碎的问题。例如,如果您在答案中使用 HiDpi 显示器会发生什么?
  • @MeetTitan 我们在这里与 AWT 合作。这些与我在 2001 年在 Java 101 中学习的类相同。因此,如果在高 DPI 显示器上发生坏事,没有人会感到惊讶。您确定可以跳过GridBagLayout 中的某个位置而不在其中添加任何内容吗?它不必是空的java.awt.Label;你会推荐什么?
  • 在这个related question 中,用户建议了一个不可见的组件。 GridBagConstraints.insets 也被讨论过,但是,只允许您设置一些像素数作为边距;它不允许您跳过网格中的单元格,当然也不兼容高 DPI。 :(
  • 我不推荐GridbagLayout,但这超出了原始问题的参数;而且 StackOverflow 并没有给我足够的报酬来考虑每个解决方案。
  • 好的,现在已经不那么老套了!我同意最初的问题是寻找GridBagLayout 解决方案。我认为不存在不涉及放置垫片的存在。只要第一列中没有组件(并且跨列 Button1 不算数!),第一列将是零宽度,导致我们看到的“缺失列”行为。我对此答案进行了研究,希望得到注意和赞赏。
猜你喜欢
  • 1970-01-01
  • 2011-04-29
  • 2021-07-27
  • 1970-01-01
  • 2014-12-06
  • 2010-10-07
  • 2015-11-14
  • 2014-02-15
  • 1970-01-01
相关资源
最近更新 更多