GridLayout以及行和列数 [英] GridLayout and number of rows and columns

查看:105
本文介绍了GridLayout以及行和列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您未完全填写GridLayout,它是否不会兑现您指定的行数和列数?

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

我正在创建一个3行4列的GridLayout.但是,我仅添加9个组件.最后以3x3网格显示了这9个组件,而不是3x4网格(第三行只有1个组件(两个空白)).

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)).

推荐答案

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

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);
    }
}

这会将它们布置为

0 1 2 3
4 5 6 7
9    

这篇关于GridLayout以及行和列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆