GridBagLayout无法正确布局 [英] GridBagLayout cant get layout right

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

问题描述

我是GridBagLayout的新手,但我尝试使用标准惯例,我可以在b $ b找到哪个是在一张网格纸上绘制我的想法,然后尝试将网格值转换为gridbag .. 。

I'm new to GridBagLayout but I tried to use the standard convention that I could find which was to draw out my idea on a piece of grid paper and then try and translate the grid values into gridbag...

我的目标是制作如下所示的布局:

my goal is to make the layout like you see below:

它目前看起来像这样:

任何想法为什么?

如果你想到一个左上角为0,0
的网格,我正在寻找的确切尺寸

the exact dimensions i'm looking for if you think of a grid with the upper left hand corner being 0,0 for


  1. 目标图片中面板中的红色:从第0列开始,跨越10列,高度为1行
  2. $ b黑色面板的$ b
  3. :从第0列,第1行,第10行开始,高度为20行

  4. 为蓝色面板:从第0列第21行开始,跨度10列,高度为1

  5. for绿色列:从列10开始,第0行,第16列,高度为7

  6. 为紫色列:从第10列开始,第7行,第16列,第16列,高度为16

  1. the red in panel in the goal picture: start at column 0, span 10 columns, with height 1 row
  2. for the black panel: start at column 0, row 1, span 10 columns, with height 20 rows
  3. for the blue panel: start at column 0, row 21, span 10 columns, with height 1
  4. for the green column: start at column 10, row 0, span 16 columns, with height 7
  5. for the purple column: start at column 10, row 7, span 16 columns, with height 16

这是我的源代码:

GBC是一个帮助类,扩展GridBagConstraints,使用的构造函数是

GBC is a helper class that extends GridBagConstraints, the constructor used is

GBC(int startingX,int startingY,int width,int height)

GBC(int startingX,int startingY,int width,int height)

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Demo extends JApplet
{

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();

    public void init()
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                initComponents();
            }
        });
    }

    public void initComponents()
    {
        //set the layout of the content pane to gridbag layout
        GridBagLayout gridBag = new GridBagLayout();
        getContentPane().setLayout(gridBag);

        Rectangle rect = getContentPane().getBounds();

        panel1.setBackground(Color.green);
        panel2.setBackground(Color.black);
        panel3.setBackground(Color.red);
        panel4.setBackground(Color.orange);
        panel5.setBackground(Color.yellow);

        add(panel4, new GBC(10, 0, 16, 7).setFill(GBC.BOTH).setWeight(1.0, 1.0));
        add(panel1, new GBC(0, 0, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0));
        add(panel3, new GBC(0, 21, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0));
        add(panel2, new GBC(0, 1, 10, 20).setFill(GBC.BOTH).setWeight(1.0, 1.0));
        add(panel5, new GBC(10, 7, 16, 16).setFill(GBC.BOTH).setWeight(1.0, 1.0));
    }
}

任何帮助都会受到赞赏(但请解释你的逻辑)

any help would be appreciated (but please explain your logic)

推荐答案

您可以使用 GridBagLayout 轻松完成此操作。

Well you can easily accomplish this by using a GridBagLayout.

考虑 contentPane ,其中 GridBagLayout 作为其布局,现在可以将 JPanel 分成三部分。单个左侧 JPanel (使用GridBagLayout,其中包含 RED BLACK BLUE JPanel s),右边包含两个 JPanel s,即 GREEN MAGENTA

Considering contentPane with GridBagLayout as its layout, you can divide the JPanel into three parts now. A single left side JPanel (with GridBagLayout, and which will contain those RED, BLACK and BLUE JPanels), and the right side consisting of two JPanels, i.e. GREEN and MAGENTA.

 ------------------------
|  left   | green JPanel |
| JPanel  |______________|
|  with   |magenta Jpanel|
|3 JPanel |              |
|________________________|

现在左边的 JPanel 将放置 RED BLACK BLUE JPanel 本身, GridBagLayout 作为布局管理器 RED和BLUE weighty = 0.1 BLACK weighty = 0.8

Now the left JPanel will place RED, BLACK and BLUE JPanel on itself, with GridBagLayout as its Layout Manager with RED and BLUE having weighty = 0.1, and BLACK having weighty = 0.8

查看此示例代码:

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

public class GridBagExample {
    private JPanel leftPanel;
    private JPanel redPanel;
    private JPanel blackPanel;
    private JPanel bluePanel;
    private JPanel greenPanel;
    private JPanel magentaPanel;

    private GridBagConstraints gbc;

    public GridBagExample() {
        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 = new JPanel(new GridBagLayout());

        leftPanel = getPanel(Color.WHITE);
        leftPanel.setLayout(new GridBagLayout());
        redPanel = getPanel(Color.RED.darker());
        blackPanel = getPanel(Color.BLACK);
        bluePanel = getPanel(Color.CYAN.darker().darker());
        greenPanel = getPanel(Color.GREEN.darker().darker());
        magentaPanel = getPanel(Color.MAGENTA);
        /**
          * @param : 
          * leftPanel : JPanel (with GridBagLayout), on which
          *               all other components will be placed.
          * redPanel : JPanel, which will be added to the leftPanel
          * 0 : specifies the grid X, which in this case is 0
          * 0 : specifies the grid Y, which in this case is 0
          * 1 : specifies the width for this grid (cell), we keeping 
          *     this default as 1
          * 1 : specifies the height for this grid (cell), we keeping
          *     this default as 1
          * GridBagConstraints.BOTH : allows JPanel to expand in both
          *     directions as the containing container expands (in 
          *     this case redPanel will expand both HORIZONTALLY and
          *     VERTICALLY, as leftPanel will expand)
          * weightx : This is the actual width the redPanel will occupy
          *           relative to all other components on the leftPanel
          * weighty : This is the actual height the redPanel will occupy
          *           relative to all other components on the leftPanel
          */
        addComp(leftPanel, redPanel, 0, 0, 1, 1,
                        GridBagConstraints.BOTH, 1.0, 0.1);
        addComp(leftPanel, blackPanel, 0, 1, 1, 1,
                        GridBagConstraints.BOTH, 1.0, 0.8);
        addComp(leftPanel, bluePanel, 0, 2, 1, 1,
                        GridBagConstraints.BOTH, 1.0, 0.1);
        addComp(contentPane, leftPanel, 0, 0, 1, 2,
                        GridBagConstraints.BOTH, 0.5, 1.0);
        addComp(contentPane, greenPanel, 1, 0, 1, 1,
                        GridBagConstraints.BOTH, 0.5, 0.3);
        addComp(contentPane, magentaPanel, 1, 1, 1, 1,
                        GridBagConstraints.BOTH, 0.5, 0.7);

        frame.setContentPane(contentPane);
        /*
         * Once you will add components to these
         * JPanels, then use pack(), instead of
         * setSize(). The use of the latter is 
         * just for illustration purpose only
         */
        //frame.pack();
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                            int x, int y, int width, int height,
                            int fill, double weightx, double weighty) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;

        panel.add(comp, gbc);
    }

    private JPanel getPanel(Color backColor) {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(backColor);

        return panel;
    }

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

以下是相同的输出:

这篇关于GridBagLayout无法正确布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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