如何使用GridBagConstraints创建布局? [英] How to use GridBagConstraints to create the layout?

查看:365
本文介绍了如何使用GridBagConstraints创建布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样布局JPane:

I want to layout my JPane like so:

-------
|     |
|     |
|     |
-------
|     |
-------

这样,顶部比底部更大/更小(顶部由另一个JPanel组成,并使用Graphics对象显示图像,而底部也由另一个JPanel组成,但使用Graphics对象来显示图像画一些线和文字).

This way, the top section is bigger/taller than the bottom section (the top section consists of another JPanel and uses the Graphics object to display an image, while the bottom section also consists of another JPanel but uses the Graphics object to draw some lines and text).

我听说最好的方法是使用GridBagLayout和GridBagConstraints.

I've heard that the best way to do this was using the GridBagLayout and GridBagConstraints.

我试图找出GridBagConstraints的适当属性,但遇到了一些困难.这就是我到目前为止所拥有的...

I'm trying to figure out the appropriate properties for the GridBagConstraints, and I'm having some difficulties. This is what I have so far...

对于顶部,我有:

gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH

对于底部,我有:

gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END

不幸的是,所有的结果都是一个大的灰色矩形出现(我的应用程序有白色背景)-没有图像加载,没有行/文本出现.

Unfortunately, all the ends up happening is a large gray rectangle appears (I have a white background for the application) - no images load, no lines/text appear.

我该怎么办?我该怎么调整?

What should I do? What should I adjust?

我已经阅读了一些教程,但看起来确实很令人困惑,我在第一个应用程序中就可以使用它,但是现在当我尝试执行此操作时,对我来说似乎不起作用.

I've read a few tutorials, but it just seems really confusing, I got it working in my first application, but now when I try to do this it just doesn't seem to work for me.

推荐答案

通常,用于网格布袋布局

In general, for gridbag layout

  • 如果要使用组件比例,则必须为其比例方向指定一个权重,并且为该方向设置的任何大小(宽度/高度)都将被布局管理器忽略.

  • if you want a component scale, you must give its scale direction a weight, and any sizes (width/height) you set for that direction will be ignored by the layout manager.

如果您不希望组件缩放,则必须定义组件的大小(如果需要,可以在Java文档中深入探讨此主题).对于底部面板,至少需要给其一个首选高度.

If you don't want a component scale, the component must have its size defined (if you want, you can dig into this topic in documents of java). In your case of the bottom panel, you need to give its, at least, a preferred height.

这可以按您的期望

pnlTop.setBackground(Color.WHITE);
pnlBottom.setBackground(Color.BLUE);

// Because you don't want the bottom panel scale, you need to give it a height.
// Because you want the bottom panel scale x, you can give it any width as the
// layout manager will ignore it.
pnlBottom.setPreferredSize(new Dimension(1, 20));


getContentPane().setLayout(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
cst.fill = GridBagConstraints.BOTH;
cst.gridx = 0;
cst.gridy = 0;
cst.weightx = 1.0; // --> You miss this for the top panel
cst.weighty = 1.0;
getContentPane().add(pnlTop, cst);

cst = new GridBagConstraints();
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = 1;
cst.weightx = 1.0; // You miss this for the bottom panel
cst.weighty = 0.0;
getContentPane().add(pnlBottom, cst);


此外,如果您想使用gridbag布局,建议您尝试使用painless-gridbag库


Further more, if you want to use gridbag layout, I recommend you to try the painless-gridbag library http://code.google.com/p/painless-gridbag/ (I'm the author of that library). It doesn't solve this problem for you (as your problem concerns managing component's size in gridbag layout) but it will save you a lot of typing and make your code easier to maintain

pnlBottom.setPreferredSize(new Dimension(1, 20));

PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);
gbl.row().cell(pnlTop).fillXY();
gbl.row().cell(pnlBottom).fillX();
gbl.done();

这篇关于如何使用GridBagConstraints创建布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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