我对 GridBag 布局管理器有一点问题。我正在尝试显示 9 个这样的面板:
为此,我像这样分隔网格:
对于面板 1 到 7,没有问题,它们会按照我的意愿显示。但问题始于面板S8
和S9
. 如您所见,S8
andS9
占据了一半的帧,但我不能让它像这样显示。S8
开头的结尾S4
和S9
结尾的开头S4
,我无法处理半个空格。
我想出的唯一方法是将S8
andS9
面板放在另一个面板中,该面板占据所有框架宽度 - 但肯定必须有一种正确的方法来显示这两个面板而不将它们放在另一个面板中!
这是代码:
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);
欢迎任何想法和建议!