GridBagLayout行跨度问题

时间:2015-02-24 13:17:38

标签: java gridbaglayout

我在使用GridBagLayout时遇到了一个奇怪的问题。我把它简化为一个简单的例子,有4个面板,2个cols和3个行。面板具有优选的尺寸,使其成为正方形(1和4)或矩形(2和3)。

   Expected                  Real result
---------------            ---------------
|  1   |  3   |            |  1   |  3   |
|      |      |            |      |      |
|      |      |            |      |      |
|------|      |            |------|      |
|  2   |      |            : space|      |
|      |      |            |------|      |
|      |------|            |  2   |------|
|      |  4   |            |      | space:
|      |      |            |      |------|
|      |      |            |      |  4   |
---------------            |      |      |
                           |      |      |
                           ---------------
                           : extra space :
                           ...............

按编号顺序添加面板会产生不良结果(实际上不是预期的结果),而将添加顺序更改为1-3-2-4会产生良好的结果。 水平移调最终会得到类似的结果。 我无法弄清楚为什么添加顺序很重要,也不知道为什么行与面板之间的额外空间重叠。

我的逻辑中缺少什么? 如果没有“添加订单”注意事项,我怎样才能达到预期的结果?

(我尝试了MigLayout及其单元格放置功能:它落在同一个洞中,顺序不会改变任何东西)

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestLayout
{
  public static void main(String[] args)
  {
    JFrame frame = new JFrame("test gridbag");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridBagLayout());

    Dimension rectDim = new Dimension(100, 200);
    Dimension carDim = new Dimension(100, 100);

    JPanel panel1 = new JPanel();
    panel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panel1.add(new JLabel("1"));
    panel1.setPreferredSize(carDim);
    GridBagConstraints c1 = new GridBagConstraints();
    c1.gridx = 0;
    c1.gridy = 0;
    c1.gridheight = 1;
    contentPane.add(panel1, c1);

    JPanel panel2 = new JPanel();
    panel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panel2.add(new JLabel("2"));
    panel2.setPreferredSize(rectDim);
    GridBagConstraints c2 = new GridBagConstraints();
    c2.gridx = 0;
    c2.gridy = 1;
    c2.gridheight = 2;
    contentPane.add(panel2, c2);

    JPanel panel3 = new JPanel();
    panel3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panel3.add(new JLabel("3"));
    panel3.setPreferredSize(rectDim);
    GridBagConstraints c3 = new GridBagConstraints();
    c3.gridx = 1;
    c3.gridy = 0;
    c3.gridheight = 2;
    contentPane.add(panel3, c3);

    JPanel panel4 = new JPanel();
    panel4.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    panel4.add(new JLabel("4"));
    panel4.setPreferredSize(carDim);
    GridBagConstraints c4 = new GridBagConstraints();
    c4.gridx = 1;
    c4.gridy = 2;
    c4.gridheight = 1;
    contentPane.add(panel4, c4);

    // TODO change add order to 1-3-2-4 for expected result

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

0 个答案:

没有答案