java GridBagLayout:如何填充所有空白空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17766217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:55:15  来源:igfitidea点击:

GridBagLayout: how to fill all empty spaces

javaswinggridbaglayout

提问by giozh

I've have a JFrame contains some JPanels using a gridBagLayout (3 rows, one column). That's my code:

我有一个 JFrame 包含一些使用 gridBagLayout(3 行,一列)的 JPanel。那是我的代码:

Container main_container = getContentPane();
GridBagLayout layout = new GridBagLayout();
main_container.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();

StatoMagazzini jpanel_stato_magazzini = new StatoMagazzini();
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.BOTH;
layout.setConstraints(jpanel_stato_magazzini, c);

AcquistoLotto jpanel_acquisto = new AcquistoLotto(i, jpanel_stato_magazzini);
c.gridx = 1;
c.gridy=1;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
layout.setConstraints(jpanel_acquisto, c);

ButtonPanel jpanel_button_panel = new ButtonPanel(i);
c.gridx=1;
c.gridy=3;
c.anchor = GridBagConstraints.CENTER;
layout.setConstraints(jpanel_button_panel, c);

main_container.add(jpanel_acquisto);
main_container.add(jpanel_stato_magazzini);
main_container.add(jpanel_button_panel);
pack();

and that's the result (a ugly result): https://docs.google.com/file/d/0Bxi2arJ2Dv9xbEo0Smd5QUN4UGc/edit?usp=sharing

这就是结果(一个丑陋的结果):https: //docs.google.com/file/d/0Bxi2arJ2Dv9xbEo0Smd5QUN4UGc/edit?usp=sharing

i would eliminate that empty spaces on top and extend the second component (that is a scrollable JTable). How i should modify code?

我会消除顶部的空白空间并扩展第二个组件(即可滚动的 JTable)。我应该如何修改代码?

采纳答案by Joris W

you could set the layout of the container to BorderLayout (or something equivalent) and create an extra JPanel with the GridBagLayout and add it to the container

您可以将容器的布局设置为 BorderLayout(或等效的东西)并使用 GridBagLayout 创建一个额外的 JPanel 并将其添加到容器中

because of Borderlayout, the JPanel will take as much space as possible

由于 Borderlayout,JPanel 将占用尽可能多的空间

回答by VGR

When a GridBagLayout has more space than it needs, it distributes that extra space using the weightxand weightyproperties of each cell. If no cells have a non-zero weight property, the extra space is not allocated to any cell, and instead all cells are centered, and sized to their preferred width/height.

当 GridBagLayout 的空间超出其需要时,它会使用每个单元格的weightxweighty属性分配额外的空间。如果没有单元格具有非零权重属性,则不会将额外空间分配给任何单元格,而是所有单元格都居中,并按其首选宽度/高度调整大小。

If you do c.weighty = 1for the constraints used by the component containing the JTable, that component will be allocated all extra vertical space. You may also want to do c.weightx = 1so the table will fill all horizontal space.

如果您c.weighty = 1对包含 JTable 的组件使用的约束执行此操作,则该组件将分配所有额外的垂直空间。您可能还想这样c.weightx = 1做,表格将填满所有水平空间。