GridBagLayout 填充

发布于 2024-08-19 09:47:15 字数 1371 浏览 10 评论 0原文

我正在使用 GridBagLayout(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在尝试学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大间隙(请参见下面的图像和代码): 替代文本 http://www.imagechicken.com/uploads/1264533379009864500.png

Image background;
    public Table(){
        super();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
        background = ii.getImage();
        setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);

        setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        JButton button = new JButton("hello world");
        JPanel panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(800,100));
        panel1.add(button, BorderLayout.CENTER);
        panel1.setBackground(Color.yellow);
        add(panel1, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;

        JPanel middlePanel = new JPanel();
        middlePanel.setPreferredSize(new Dimension(800,350));
        middlePanel.add(button, BorderLayout.CENTER);
        middlePanel.setBackground(Color.blue);
        add(middlePanel, constraints);


    }

I am using a GridBagLayout to (currently) display two rows. I am aware this layout is overkill for this task, but am trying to learn how to use it. The problem is that I have added the two panels to the two separate rows and there is a huge gap around the content (see image and code below):
alt text http://www.imagechicken.com/uploads/1264533379009864500.png

Image background;
    public Table(){
        super();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
        background = ii.getImage();
        setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);

        setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        JButton button = new JButton("hello world");
        JPanel panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(800,100));
        panel1.add(button, BorderLayout.CENTER);
        panel1.setBackground(Color.yellow);
        add(panel1, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;

        JPanel middlePanel = new JPanel();
        middlePanel.setPreferredSize(new Dimension(800,350));
        middlePanel.add(button, BorderLayout.CENTER);
        middlePanel.setBackground(Color.blue);
        add(middlePanel, constraints);


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2024-08-26 09:47:15

使用

constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;

JavaDoc 表示 weightx/重量级 说:

指定如何分配额外的水平/垂直空间。

JavaDoc for fill< /代码>:

当组件的显示区域较大时使用该字段
比组件要求的尺寸。它决定是否
调整组件的大小,如果是,如何调整。

Use

constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;

JavaDoc for weightx/weighty says:

Specifies how to distribute extra horizontal/vertical space.

JavaDoc for fill:

This field is used when the component's display area is larger
than the component's requested size. It determines whether to
resize the component, and if so, how.

逆夏时光 2024-08-26 09:47:15

不幸的是,对于 GridBagLayout,如果内容没有填满其所在的整个容器,它会自动将其所有内容在其容器内居中。这就是为什么你会出现很大的差距。

本质上有两种方法可以解决这个问题:

  1. 困难的方法:摆​​弄 GridBagConstraints 。当我试图避免居中行为时,我在这方面取得的成功有限。
  2. 简单的方法:将 GridBagLayout 放在 FlowLayout 中,然后将 FlowLayout 的对齐方式设置为左上角,或者任何你想要的。

上周我自己问过并回答了这个问题

因此,在您的情况下,您将 panel1middlePanel 直接添加到 JFrame (?),并使用 GridBagLayout

JFrame (GridBagLayout)
 - panel1
 - middlePanel

我建议使用这种替代结构,消除所有多余的空间(如果您愿意,也可以中心对齐)。

JFrame (FlowLayout)
 - JPanel (GridBagLayout)
   - panel1
   - middlePanel

华泰

Unfortunately, with GridBagLayout, if the contents do not fill the entire container that it is in, it will automatically center all its contents within its container. That is why you are getting a really large gap.

There are essentially two ways to fix this:

  1. The hard way: Fiddle with the GridBagConstraints. I had limited success with this when trying to avoid the centre-ing behaviour.
  2. The easy way: Put the GridBagLayout inside of a FlowLayout, and then set the alignment of the FlowLayout to top-left, or whatever you wish.

I have asked, and answered, this question myself sometime last week.

So in your case you are adding your panel1 and middlePanel directly to the JFrame (?), with a GridBagLayout

JFrame (GridBagLayout)
 - panel1
 - middlePanel

I would suggest this alternative structure instead, to get rid of all the extra space (and centre alignment as well, if you like).

JFrame (FlowLayout)
 - JPanel (GridBagLayout)
   - panel1
   - middlePanel

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文