2

我想像这样布局我的 JPane:

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

这样,顶部比底部更大/更高(顶部由另一个 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

对于底部,我有:

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

不幸的是,所有最终发生的事情都是出现一个大的灰色矩形(我的应用程序有一个白色背景) - 没有加载图像,没有出现线条/文本。

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

我已经阅读了一些教程,但这似乎真的很混乱,我在我的第一个应用程序中得到了它,但现在当我尝试这样做时,它似乎对我不起作用。

4

3 回答 3

5

一般来说,对于gridbag布局

  • 如果你想要一个组件比例,你必须给它的比例方向一个权重,你为那个方向设置的任何尺寸(宽度/高度)都将被布局管理器忽略。

  • 如果您不想要组件缩放,则必须定义组件的大小(如果需要,您可以在 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);

此外,如果您想使用 gridbag 布局,我建议您尝试使用 painless-gridbag 库http://code.google.com/p/painless-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();
于 2012-01-26T05:45:37.440 回答
2

从你的问题中不确定,也许会帮助你 70% 最高,30% 最低

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();
        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        getContentPane().add(panel1, gbc);
        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = 30;
        gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        getContentPane().add(panel2, gbc);
        pack();
    }

    public static void main(String[] args) {
        new BorderPanels().setVisible(true);
    }
}
于 2012-01-26T08:02:35.187 回答
0

如果您可以使用第 3 方库,您也可以考虑使用FormLayout。它允许您指定每行的高度和调整大小的行为。

我没有对此进行测试,但是

FormLayout layout = new FormLayout( 
 "pref", //columns
 "50dlu:grow(0.2)","25dlu:grow(0.1)" //rows
);

可能会成功。有关语法的更多信息,请参阅 JGoodies Forms pdf

于 2012-01-26T07:35:32.273 回答