【问题标题】:How to use GridBagConstraints to create the layout?如何使用 GridBagConstraints 创建布局?
【发布时间】:2012-02-19 07:22:35
【问题描述】:

我想像这样布置我的 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

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

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

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

【问题讨论】:

  • 您能提供一个SSCCE 来说明问题吗?从您列出的内容来看,甚至不清楚面板是否被添加到任何东西......
  • 请发布您用于设置窗格的整个代码。
  • -1, I've heard that the best way to do this was using the GridBagLayout and GridBagConstraints. 这不是你 3 小时前问这个问题时得到的建议:stackoverflow.com/questions/9012976/…
  • @camickr 这不是我听说的,而且这不适用,因为您无法为 GridLayout 指定高度。
  • 使用BorderLayout。如果两个组件之一应该扩展,将其放在CENTER 中,否则使用NORTH/SOUTH。我认为GridBagLayout 对这种布局来说是个坏建议。你从哪里听到的?

标签: java swing gridbaglayout


【解决方案1】:

一般来说,对于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();

【讨论】:

    【解决方案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);
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      我没有测试这个,但是

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-27
        • 2014-05-14
        • 2020-01-21
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多