GridBagLayout:如何填充所有空格 [英] GridBagLayout: how to fill all empty spaces

查看:233
本文介绍了GridBagLayout:如何填充所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

推荐答案

你可以将容器的布局设置为BorderLayout(或类似的东西)并创建一个额外的带有GridBagLayout的JPanel并将其添加到容器

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,JPanel将占用尽可能多的空间

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

这篇关于GridBagLayout:如何填充所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆