简体   繁体   English

Java Swing:GridBagLayout,如何将4个按钮压缩为两列?

[英]Java Swing: GridBagLayout, how to squeeze 4 buttons to two columns?

so in my japanel, I have a componenet which is 2 columns wide. 所以在我的japanel中,我有一个comonenet,它是2列宽。 I would like to add 4 buttons at the bottom not exceeding this 2 column. 我想在底部添加4个按钮,但不超过这2列。 however, what I get is all 4 buttons stretch out across to 4 columns, exceeding two clumns. 但是,我得到的是所有4个按钮延伸到4列,超过了两个提示。 the cancelButton also has a weird horizontal elongation . cancelButton还具有怪异的水平伸长率。

        gbc.gridx = 0;
    gbc.gridy = 0;  
    gbc.gridwidth = 2;
    gbl.setConstraints(someComponenetHere, gbc);                    

    gbc.gridy = 2;  
    gbl.setConstraints(applyButton, gbc);

    gbc.gridx = 1;
    gbc.gridy = 2;  
        gbc.gridwidth = 1;
    gbl.setConstraints(cancelButton, gbc);

    gbc.gridx = 2;
    gbc.gridy = 2;  
    gbc.gridwidth = 1;
    gbl.setConstraints(applyAddButton, gbc);

    gbc.gridx = 3;
    gbc.gridy = 2;  
    gbc.gridwidth = 1;
    gbl.setConstraints(applyAddAllButton, gbc);

My approach would be to make you component 4 columns wide and stick a button in each column. 我的方法是使您的组件宽4列,并在每列中粘贴一个按钮。

Failing that you could create a Panel containg 2 buttons and stick one of those panels in each column. 未能创建一个包含2个按钮的面板,并将其中一个面板粘贴在每列中。

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

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