Bha*_*axy 2 java swing gridbaglayout
我想像我这样布局我的JPane:
-------
| |
| |
| |
-------
| |
-------
Run Code Online (Sandbox Code Playgroud)
这样,顶部比底部更大/更高(顶部由另一个JPanel组成并使用Graphics对象显示图像,而底部也包含另一个JPanel但使用Graphics对象绘制一些线条和文字).
我听说最好的方法是使用GridBagLayout和GridBagConstraints.
我试图找出GridBagConstraints的适当属性,我遇到了一些困难.这就是我到目前为止......
对于顶部,我有:
gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH
Run Code Online (Sandbox Code Playgroud)
对于底部,我有:
gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END
Run Code Online (Sandbox Code Playgroud)
不幸的是,所有最终结果都出现了一个大的灰色矩形(我有一个白色背景的应用程序) - 没有图像加载,没有行/文本出现.
我该怎么办?我应该调整什么?
我已经阅读了一些教程,但它看起来真的很混乱,我在第一个应用程序中使用它,但现在当我尝试这样做时它似乎对我不起作用.
一般来说,用于格子袋布局
如果需要组件比例,则必须为其比例方向指定权重,布局管理器将忽略为该方向设置的任何大小(宽度/高度).
如果您不想要组件比例,则组件必须定义其大小(如果需要,您可以在java的文档中深入研究此主题).对于底部面板,您需要至少给出一个首选高度.
这可以满足您的期望
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);
Run Code Online (Sandbox Code Playgroud)
此外,如果你想使用gridbag布局,我建议你试试painless-gridbag库http://code.google.com/p/painless-gridbag/(我是该库的作者).它并没有为你解决这个问题(因为你的问题是关于在gridbag布局中管理组件的大小)但它会为你节省大量的打字并使你的代码更容易维护
pnlBottom.setPreferredSize(new Dimension(1, 20));
PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);
gbl.row().cell(pnlTop).fillXY();
gbl.row().cell(pnlBottom).fillX();
gbl.done();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12098 次 |
| 最近记录: |