网格包布局没有显示我想要的方式 [英] Grid bag layout not displaying the way I want

查看:109
本文介绍了网格包布局没有显示我想要的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有6个面板,所有面板都使用网格布局。
我用gridbaglayout将它们放在一起,这是我想要的设计

So I have 6 panels, all of them used a grid layout. And I put them together using gridbaglayout, here is the design I wanted

结果它变得一团糟
第二面板变得更加偏向右边
第三个面板在左边挤了很多,这是一场灾难。
这是格子袋布局的代码

Turn out it became a total mess The "second" panel became much further away to the right The third panel is squeezed a lot to the left and it was a disaster. Here is my code for the gridbag layout

    c.gridx = 0; c.gridy = 0;
    add (first,c); 

    c.gridx = 2; //so that the second panel starts from the center and is divided evenly with the first panel
    add(second,c);

    c.gridx = 0; c.gridy = 1;
    add(third,c);

    c.gridx = 1; 
    add(fourth,c);

    c.gridx = 2;
    add(fifth,c);

    c.gridx = 3;
    add(sixth,c);

感谢任何帮助。

推荐答案

当您编写 c.gridx = 0和c.gridy = 0 时,您忘记指定您想要的列数 JPanel 占据。为了指定它,你应该使用 c.gridwidth = 2 ,它告诉布局这个 JPanel 需要两列空间。

As you wrote c.gridx = 0 and c.gridy = 0, you forgot to specify how many columns you want this JPanel to occupy. In order to specify that, you should be using c.gridwidth = 2, which tells the layout that this JPanel needs space for two columns.

要得到这样的输出:

这是一个小工作示例:

import java.awt.*;
import javax.swing.*;
// http://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other
public class GridBagLayoutExample
{
    // http://stackoverflow.com/questions/10977017/grid-bag-layout-not-displaying-the-way-i-want
    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        gbc.weighty = 0.2;

        JPanel topLeftPanel = new JPanel();
        topLeftPanel.setOpaque(true);
        topLeftPanel.setBackground(Color.DARK_GRAY);
        contentPane.add(topLeftPanel, gbc);

        gbc.gridx = 2;

        JPanel topRightPanel = new JPanel();
        topRightPanel.setOpaque(true);  
        topRightPanel.setBackground(Color.BLUE);
        contentPane.add(topRightPanel, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.25;
        gbc.weighty = 0.8;

        JPanel firstPanel = new JPanel();
        firstPanel.setOpaque(true);
        firstPanel.setBackground(Color.RED);
        contentPane.add(firstPanel, gbc);

        gbc.gridx = 1;

        JPanel secondPanel = new JPanel();
        secondPanel.setOpaque(true);
        secondPanel.setBackground(Color.GREEN.darker());
        contentPane.add(secondPanel, gbc);

        gbc.gridx = 2;

        JPanel thirdPanel = new JPanel();
        thirdPanel.setOpaque(true);
        thirdPanel.setBackground(Color.WHITE);
        contentPane.add(thirdPanel, gbc);

        gbc.gridx = 3;

        JPanel fourthPanel = new JPanel();
        fourthPanel.setOpaque(true);
        fourthPanel.setBackground(Color.MAGENTA);
        contentPane.add(fourthPanel, gbc);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutExample().displayGUI();
            }           
        });
    }
}

这篇关于网格包布局没有显示我想要的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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