首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在交替列GridBagLayout中设置组件

在交替列GridBagLayout中设置组件
EN

Stack Overflow用户
提问于 2017-04-05 19:48:39
回答 2查看 55关注 0票数 0

是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中使用GridBagConstraints填充1、3、5中的空组件?

这里的Java示例将第5个按钮设置为从第1列开始,但可能是因为上面的行已经设置好了吗?

https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-05 19:53:40

是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中的空组件填充1、3、5

不是的。如果没有为一个单元定义组件,那么它的大小基本上是0。

布局无法猜测“空列”的大小。

也许您可以使用“inset”约束在列之间留出空间。

票数 2
EN

Stack Overflow用户

发布于 2017-04-06 10:16:23

可以用行高和列宽定义GridBagLayout。

代码语言:javascript
复制
int[]  rowHeights = new int[] { 50, 50, 50};
int[] columnWidths = new int[] { 100, 100, 100, 100, 100};
GridBagLayout layout = new GridBagLayout();
layout.rowHeights = rowHeights;
layout.columnWidths = columnWidths;

使用GridBagConstraint添加组件,如下所示

代码语言:javascript
复制
    JPanel panel = new JPanel();
    panel.setLayout(layout);

    JLabel label1 = new JLabel("Label 1");
    JLabel label2 = new JLabel("Label 2");
    JLabel label3 = new JLabel("Label 3");

    panel.add(label1,  new GridBagConstraints(
            0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
            new Insets(0,0, 0, 0), 0, 0));

    panel.add(label2,  new GridBagConstraints(
            2, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
            new Insets(0,0, 0, 0), 0, 0));

    panel.add(label3,  new GridBagConstraints(
            4, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
            new Insets(0,0, 0, 0), 0, 0));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43240363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档