繁体   English   中英

Java GUI GridBagLayout 如何在一列中容纳三个组合框?

[英]Java GUI GridBagLayout how to fit three combo box in one column?

我正在使用 Eclipse,用 Java 编程,使用 GridBagLayout 布局管理器进行学校项目。

到目前为止它看起来像这样

在此处输入图片说明

我想将三个组合框(MM、DD、YYYY)放在一列(与 accNoT 和其他人对齐)但我不知道我是如何尝试修改 weightx 或 weighty 但我什么也没做

这是该部分的代码

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipadx = 50;
pane.add(accNoT, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(cellNoT, c);



c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;
c.gridx = 1;
c.gridy = 2;
pane.add(monthBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;

c.gridx = 2;
c.gridy = 2;
pane.add(dayBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0.3;
c.gridx = 3;
c.gridy = 2;
pane.add(yearBox, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
pane.add(planList, c);

我在图像中添加了一些文字,所以它不会有点混乱 ps。 我正在关注https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html我刚刚修改了他们的例子

到目前为止,除了试图找出 weightx 和 y 之外我还做了什么,直到现在我仍然不明白我将 DD 和 YYYY 组合框放在不同的列然后我将一些 gridWidth 设置为 accNoT 等于 2 但它没有用, DD 就消失了

回答非常感谢 user85421///////////////////////////

就像user85421在评论中说的,添加gridwidth三个组合框应该是1,然后剩下的应该是3

编码

  c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipadx = 50;
c.gridwidth = 3;
pane.add(accNoT, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 3;

pane.add(cellNoT, c);



c.fill = GridBagConstraints.HORIZONTAL;


c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;

pane.add(monthBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weighty = 0.3;

c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
pane.add(dayBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weighty = 0.3;
c.gridx = 3;
c.gridy = 2;
c.gridwidth = 1;
pane.add(yearBox, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 3;
pane.add(planList, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 3;
pane.add(adjT, c);

c.fill = GridBagConstraints.HORIZONTAL;

//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 5;
c.gridwidth = 3;
pane.add(eUsageT, c);

c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 6;
c.gridwidth = 3;
pane.add(prevBAT, c);

////THIRD COLUMN//////////////////////////////////////////
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 3;
pane.add(monPLR, c);

结果

在此处输入图片说明

是的,它看起来很可怕,但至少三个组合框可以放在一列中

您可能会在“第三列”部分注意到 gridy = 4 那是因为 YYYY 组合框的 gridy = 3

再次感谢 user85421 拯救了我们小组的生命

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM