1
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。这是行不通的。如何解决?

在此处输入图像描述

4

1 回答 1

3

您未能weightx为. 此外,您的价值观是错误的,因为它只需要最底层,其余的都需要。weightyGridBagConstraintsgridwidth2JPanel1


我在做什么的解释: 考虑JPanelsBLUERED,它们将被放置在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-AXISBLUE ),由and s占据RED JPanel,就此而言,它的gridwidth = 2and weightx = 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);
    }
}

这是相同的输出:

GridBagLayout 示例

于 2013-07-14T14:34:06.897 回答