GridBagLayout无法正常工作 [英] GridBagLayout is not working

查看:76
本文介绍了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值是错误的,因为对于最底部的JPanel只需要将其设置为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.

我在做什么的说明: 考虑JPanel s BLUERED,它们应沿着 X-AXIS 并按比例放置 相对于70:30(因此,它们的weightx将分别为0.70.3.由于沿 X-AXIS 的总面积为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轴放置,相对于第三个GREEN JPanel的比例为90:10,因此,这两个这些BLUERED中的weighty = 0.9GREEN JPanel将具有weighty = 0.1,但是由于GREEN JPanel被假定占据整个区域(相对于 X-AXIS BLUE和RED 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天全站免登陆