GridLayout 以及行数和列数

发布于 2024-11-01 20:15:19 字数 152 浏览 6 评论 0原文

如果您没有完全填充 GridLayout,它是否会不遵守您指定的行数和列数?

我正在创建一个 3 行 4 列的 GridLayout。不过,我只添加了 9 个组件。它最终在 3x3 网格中向我显示这 9 个组件,而不是 3x4 网格(第三行只有 1 个组件(和两个空白))。

Does GridLayout ever not honor the number of rows and columns you've specified if you don't fill it completely?

I'm creating a GridLayout with 3 rows and 4 columns. However, I'm only adding 9 components. It ends up showing me these 9 components in a 3x3 grid, rather than a 3x4 grid (with only 1 component on the third row (and two blanks)).

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

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

发布评论

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

评论(2

千笙结 2024-11-08 20:15:19

而不是 3x4 网格(第三行只有 1 个组件(和两个空白))。

然后你应该使用以下方法创建 GridLayout:

setLayout(new GridLayout(0,4)); 

它告诉布局你不知道有多少行,但你想要 4 列。因此,在移动到下一行之前,这些列将被填满。

不需要空组件。

rather than a 3x4 grid (with only 1 component on the third row (and two blanks)).

Then you should be creating your GridLayout using:

setLayout(new GridLayout(0,4)); 

It tell the layout that you don't know how many rows you have, but you want 4 columns. So the columns will be filled up before moving to the next row.

No need for empty components.

老子叫无熙 2024-11-08 20:15:19

只需用空项目填充空单元格(例如 JLabel),例如:

class MyFrame extends JFrame
{
    MyFrame()
    {
        setLayout(new GridLayout(3,4));

        for (int i = 0; i < 9; ++i)
            this.getContentPane().add(new JLabel(""+i));
        for (int i = 0; i < 3; ++i)
            getContentPane().add(new JLabel());

        pack();
        setVisible(true);
    }
}

这将它们布局为

0 1 2 3
4 5 6 7
9    

Just fill empty cells with empty items (like a JLabel), eg:

class MyFrame extends JFrame
{
    MyFrame()
    {
        setLayout(new GridLayout(3,4));

        for (int i = 0; i < 9; ++i)
            this.getContentPane().add(new JLabel(""+i));
        for (int i = 0; i < 3; ++i)
            getContentPane().add(new JLabel());

        pack();
        setVisible(true);
    }
}

This layouts them as

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