简体   繁体   English

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

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

I am using Eclipse, programming in Java, using GridBagLayout layout manager for a school project.我正在使用 Eclipse,用 Java 编程,使用 GridBagLayout 布局管理器进行学校项目。

So far it looks like this到目前为止它看起来像这样

在此处输入图片说明

I wanna put the three combo box (MM, DD, YYYY) in one column(aligned with accNoT and others) but I don't know how I tried tinkering with weightx or weighty but I doesn't do anything我想将三个组合框(MM、DD、YYYY)放在一列(与 accNoT 和其他人对齐)但我不知道我是如何尝试修改 weightx 或 weighty 但我什么也没做

Here's the code to that portion这是该部分的代码

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

I added some text in the image so it won't be a bit confusing ps.我在图像中添加了一些文字,所以它不会有点混乱 ps。 I am following https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html I just modified their example我正在关注https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html我刚刚修改了他们的例子

So far what I did besides trying to figure out weightx and y which up till now I still don't understand I put the DD and YYYY combo box in different column Then I set some gridWidth to accNoT equals to 2 but it didn't work, the DD just disappeared到目前为止,除了试图找出 weightx 和 y 之外我还做了什么,直到现在我仍然不明白我将 DD 和 YYYY 组合框放在不同的列然后我将一些 gridWidth 设置为 accNoT 等于 2 但它没有用, DD 就消失了

ANSWERED THANK YOU SO MUCH user85421/////////////////////////////回答非常感谢 user85421///////////////////////////

Just like user85421 said in the comments, add gridwidth the three combo box should be 1, then the rest should be 3就像user85421在评论中说的,添加gridwidth三个组合框应该是1,然后剩下的应该是3

The code编码

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

The result结果

在此处输入图片说明

Yes it looks hideous but at least the three combo box fits in a column是的,它看起来很可怕,但至少三个组合框可以放在一列中

You might notice in the "Third column" section The gridy = 4 That's because the YYYY combo box's gridy = 3您可能会在“第三列”部分注意到 gridy = 4 那是因为 YYYY 组合框的 gridy = 3

Once Again thank you user85421 for saving our group's lives再次感谢 user85421 拯救了我们小组的生命

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

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