问题描述
我对GridBag布局管理器有一点问题。我试图显示9个这样的面板:
I have a little issue with the GridBag Layout Manager. I am trying to display 9 panels like this:
为此,我将网格分开:
对于面板1到7,有没问题,他们就像我想要的那样出现。但问题始于面板 S8
和 S9
。正如你所看到的, S8
和 S9
占据了一半的帧,但我不能让它显示为这个。 S8
在 S4
的开头结束, S9
从 S4
的结尾开始,我无法处理半空间。
For the panels 1 to 7 there is no problem, they show up just as I want. But the issue starts with the panels S8
and S9
. As you can see, the S8
and S9
takes up half the frame, but I can't make it display like this. The S8
ends at the start of S4
, and the S9
begins at the end of S4
, I cannot handle the half space.
我想出的唯一方法就是将 S8
和 S9
面板放在另一个采用所有框架宽度的面板中 - 但肯定必须有正确的方法显示这两个面板而不将它们放在另一个面板中!
Only way I figured out is to put the S8
and S9
panel in another panel which takes all the frame width - but surely there must be a proper way to display these two panels without putting them in another panel!
以下是代码:
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放在底部面板中,但我喜欢使用GBL的1个面板的挑战。 [无聊]
Good question. It took me little to crack it, but I got it. Normally, I would have put setions 1-7 in a top panel, and sections 8-9 in a bottom panel, but I liked the challange of 1 panel with GBL. [bored]
问题是第4节(列索引4和5)没有为GBL定义好,所以第8节不知道到了多远去覆盖它的第五列(索引4),然后在列索引3之后停止。
The problem is that section 4 (column indexes 4 and 5) is not well-defined for GBL, so section 8 doesn't know how far out to go to cover it's fifth column (index 4) so it then just stops after column index 3.
所以,我在组成的列中添加了2个零高度的间隔符第4节,它工作。注释掉标记为SPACERS的2行,看看我的意思:
So, I added 2 zero-height spacers in the columns that make up section 4 and it worked. Comment out the 2 lines marked SPACERS to see what I mean:
编辑:@SheridanVespo建议添加修复
EDIT: added fix suggested by @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);
}
}
}
这篇关于GridBagLayout面板对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!