有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用Eclipse,用Java编程,在学校项目中使用GridBagLayout manager

目前看来是这样的

enter image description here

我想把三个组合框(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);

我在图片中添加了一些文字,这样就不会有点混乱了 另外,我正在关注https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 我只是修改了他们的例子

到目前为止,除了试图找出权重x和y之外,我还做了些什么,到目前为止我还不明白 我将DD和YYYY组合框放在不同的列中 然后我将一些gridWidth设置为accNoT等于2,但它不起作用,DD只是消失了


共 (1) 个答案

  1. # 1 楼答案

    回答:非常感谢用户85421//////

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

    结果

    enter image description here

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

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

    再次感谢用户85421拯救了我们小组的生命