GridBagLayout 不起作用 [英] GridBagLayout is not working

查看:47
本文介绍了GridBagLayout 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.这是行不通的.怎么解决?

Want to format it like this. grey part shows the jpanel part. Initially i want to layout the first 2 jpanel correctly . which is not working. how to fix it?

推荐答案

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

You are failing to specify any weightx and weighty values to the GridBagConstraints. Moreover your gridwidth values are wrong, since it only needs to be 2 for the bottom most JPanel, for the rest it needs to be 1.

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

Explanation of what I am doing : Consider JPanels BLUE and RED, they are to be placed along the X-AXIS, in the ratio 70:30, with respect to each other (therefore their weightx will be 0.7 and 0.3 respectively. Since the total area along the X-AXIS is 1.0).

现在这两个 BLUERED JPanel 都将沿着 Y-AXIS 放置,相对于第三个 GREEN JPanel 的比例为 90:10,因此,这两个 BLUERED 都会有 weighty =0.9,并且 GREEN JPanel 将具有 weighty = 0.1,但是由于 GREEN JPanel 假设占据整个区域(与相对于 X-AXIS),由 BLUERED JPanel 占用,就此而言,它的 gridwidth = 2weightx = 1.0.

Now both of these BLUE and RED JPanels are to be placed along the Y-AXIS, with respect to the third GREEN JPanel in the ratio 90:10, therefore, both of these BLUE and RED will have weighty = 0.9, and the GREEN JPanel will have weighty = 0.1, but since GREEN JPanel is suppose to occupy the whole area (with respect to X-AXIS), as occupied by BLUE and RED JPanels, for that matter, its gridwidth = 2 and 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆