【问题标题】:GridBagLayout is not workingGridBagLayout 不工作
【发布时间】:2013-07-12 11:32:09
【问题描述】:
this.rootComponent.setLayout(new GridBagLayout());
        GridBagConstraints gbc=new GridBagConstraints();

        //gbc.gridwidth=2;
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.gridwidth=8;
        gbc.anchor=GridBagConstraints.FIRST_LINE_START;

        this.rootComponent.add(new JLabel("Test label 1"),gbc);

        gbc.gridx=8;
        gbc.gridy=12;
        gbc.gridwidth=GridBagConstraints.REMAINDER;
        gbc.anchor=GridBagConstraints.FIRST_LINE_START;

        this.rootComponent.add(new JLabel("Test label"),gbc);

想要这样格式化。灰色部分显示 jpanel 部分。最初我想正确布局前 2 个 jpanel。这是行不通的。如何解决?

【问题讨论】:

标签: java swing layout layout-manager gridbaglayout


【解决方案1】:

您未能为GridBagConstraints 指定任何weightxweighty 值。此外,您的gridwidth 值是错误的,因为对于最底部的JPanel,它只需要是2,其余的它需要是1


解释我在做什么: 考虑JPanels BLUERED,它们将沿X-AXIS 放置,按比例 70:30,相对于彼此(因此它们的weightx 将分别为0.70.3。由于沿X-AXIS 的总面积为1.0)。

现在这两个BLUERED JPanels 都将沿Y 轴 放置,相对于第三个GREEN JPanel 的比率90:10,因此,两者其中BLUERED 将具有weighty = 0.9,而GREEN JPanel 将具有weighty = 0.1,但由于GREEN JPanel 假定占据整个区域(相对于X-AXIS),被BLUERED JPanels 占据,就此而言,它的gridwidth = 2weightx = 1.0


试试这个代码示例:

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

public class GridBagLayoutExample
{
    private GridBagConstraints gbc;

    public GridBagLayoutExample()
    {
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = getPanel(Color.WHITE);
        contentPane.setLayout(new GridBagLayout());

        JPanel leftPanel = getPanel(Color.BLUE);
        JPanel rightPanel = getPanel(Color.RED);
        JPanel bottomPanel = getPanel(Color.GREEN.darker());

        addComp(contentPane, leftPanel
                , 0, 0, 0.7, 0.9, 1, 1, GridBagConstraints.BOTH);
        addComp(contentPane, rightPanel
                , 1, 0, 0.3, 0.9, 1, 1, GridBagConstraints.BOTH);
        addComp(contentPane, bottomPanel
                , 0, 1, 1.0, 0.1, 2, 1, GridBagConstraints.BOTH);

        frame.setContentPane(contentPane);
        //frame.pack();
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp
                            , int gridX, int gridY
                            , double weightX, double weightY
                            , int gridWidth, int gridHeight, int fill)
    {
        gbc.gridx = gridX;
        gbc.gridy = gridY;
        gbc.weightx = weightX;
        gbc.weighty = weightY;
        gbc.gridwidth = gridWidth;
        gbc.gridheight = gridHeight;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    private JPanel getPanel(Color backColour)
    {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(backColour);
        panel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));

        return panel;
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new GridBagLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

这是相同的输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-14
    • 2014-08-21
    • 2012-11-18
    • 2013-08-22
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多