gpt4 book ai didi

java - GridBagLayout 面板对齐

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:02:37 27 4
gpt4 key购买 nike

我对 GridBag 布局管理器有一个小问题。我正在尝试像这样显示 9 个面板:

Ideal Layout - Illustrated

为此,我像这样分隔网格:

Work In Progress - Illustrated

对于面板 1 到 7 没有问题,它们显示如我所愿。但问题始于面板 S8S9。如您所见,S8S9 占据了一半的画面,但我无法让它像这样显示。 S8结束于S4开始,S9开始于S4结束,我无法处理半空间。

我想出的唯一方法是将 S8S9 面板放在另一个占据所有框架宽度的面板中 - 但肯定必须有一种正确的显示方式这两个面板没有将它们放在另一个面板中!

代码如下:

workzone.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0.5;
c.insets = new Insets(0,0,0,0);

//S1
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
workzone.add(S1, c);

//S2
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
workzone.add(S2, c);

//S3
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 2;
c.gridheight = 1;
workzone.add(S3, c);

//S4
c.gridx = 4;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
workzone.add(S4, c);

//S5
c.gridx = 7;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
workzone.add(S5, c);

//S6
c.gridx = 7;
c.gridy = 1;
c.gridwidth = 2;
c.gridheight = 1;
workzone.add(S6, c);

//S7
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 2;
workzone.add(S7, c);

//S8
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 5;
c.gridheigth = 1;
workzone.add(S8, c);

//S9
c.gridx = 6;
c.gridy = 2;
c.gridwidth = 5;
c.gridheight = 1;
workzone.add(S9, c);

欢迎任何想法和建议!

最佳答案

好问题。我花了一点时间来破解它,但我明白了。通常,我会将第 1-7 部分放在顶部面板中,将第 8-9 部分放在底部面板中,但我喜欢 1 个面板与 GBL 的挑战。 [无聊]

问题是第 4 部分(列索引 4 和 5)没有为 GBL 明确定义,因此第 8 部分不知道要覆盖它的第五列(索引 4)多远,所以它就停止了在列索引 3 之后。

因此,我在构成第 4 部分的列中添加了 2 个零高度垫片并且它起作用了。注释掉标记为 SPACERS 的 2 行以了解我的意思:

编辑:添加了@SheridanVespo 建议的修复

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GridBagDemo2 implements Runnable
{
private Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.GRAY,
Color.GREEN, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.YELLOW};

private JPanel panel;
private GridBagConstraints gbc;

public static void main(String[] args)
{
SwingUtilities.invokeLater(new GridBagDemo2());
}

public void run()
{
panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();

add(0,0, 2,2, "1");
add(2,0, 2,1, "2");
add(2,1, 2,1, "3");
add(4,0, 2,2, "4");
add(6,0, 2,1, "5");
add(6,1, 2,1, "6");
add(8,0, 2,2, "7");
add(0,2, 5,1, "8");
add(5,2, 5,1, "9");

// SPACERS: define the 2 columns that is section "4"
add(4,3, 1,1, "");
add(5,3, 1,1, "");

JFrame frame = new JFrame("Grig Bag");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}

private void add(int x, int y, int colspan, int rowspan, String name)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = colspan;
gbc.gridheight = rowspan;
gbc.weightx = .1;
gbc.weighty = .1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;

// using another panel for illustrative purposes only
JPanel p = new JPanel();

if (!name.equals(""))
{
gbc.weightx = 1; // @SheridanVespo fix
int index = Integer.parseInt(name);
JLabel label = new JLabel("Panel " + name);
p.add(label);
p.setBackground(colors[index]);
panel.add(p, gbc);
}
else
{
gbc.weightx = 0.5; // @SheridanVespo fix
gbc.weighty = 0; // don't allow the spacer to grow
gbc.fill = GridBagConstraints.NONE;
panel.add(Box.createHorizontalGlue(), gbc);
}
}
}

关于java - GridBagLayout 面板对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14755487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com