GridBagLayout对齐列不应该

时间:2014-03-26 15:12:35

标签: java swing layout-manager gridbaglayout

对不起另一个“我的代码在这里出了什么问题”的问题,但我一直在与这个GridBagOfHurtLayout战斗,我不明白可能会发生什么。

我想制作一个像这样的布局的用户界面:

What it should be

油漆程序崩溃了,我得到的只是一个缩小的截图,对于颗粒感很抱歉。所以我看到它的方式,我想要一个有4列的GridBagLayout,其中前两个窗格分别跨越2列,文本字段和大窗格跨越3,搜索按钮和左下窗格跨越1(水平)。这是我的代码:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(1080, 720));

this.setLayout(new GridBagLayout());
GridBagConstraints constraints;

projectLabel = new JLabel("Projects");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.LINE_START;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.weightx = 0.25;
constraints.weighty = 0;
constraints.insets = new Insets(4,4,4,4);
this.add(projectLabel);

projectScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2; 
constraints.weightx = 1.0;
constraints.weighty = 0.5;
constraints.insets = new Insets(4,4,4,4);
projectScrollPane.setPreferredSize(new Dimension(540, 240));
this.add(projectScrollPane,constraints);

fieldLabel = new JLabel("Fields");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.LINE_START;
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 0;
constraints.insets = new Insets(4,4,4,4);
this.add(fieldLabel);

fieldScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 2;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.weightx = 1.0;
constraints.weighty = 0.5;
constraints.insets = new Insets(4,4,4,4);
fieldScrollPane.setPreferredSize(new Dimension(540, 240));
this.add(fieldScrollPane,constraints);

searchTextField = new JTextField();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.weightx = 2.0;
constraints.weighty = 0.0;
constraints.insets = new Insets(4,4,4,4);
searchTextField.setPreferredSize(new Dimension(1000,30));
this.add(searchTextField,constraints);

searchButton = new JButton("Search");
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.NONE;
constraints.gridx = 3;
constraints.gridy = 2;
constraints.weightx = 0.0;
constraints.weighty = 0.0;
constraints.insets = new Insets(4,4,4,4);
searchButton.setSize(new Dimension(80,30));
this.add(searchButton,constraints);

imageScrollPane = new JScrollPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 3;
constraints.weightx = 0.25;
constraints.weighty = 1.0;
constraints.insets = new Insets(4,4,4,4);
imageScrollPane.setPreferredSize(new Dimension(360,810));
this.add(imageScrollPane,constraints);

imagePane = new JEditorPane();
constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 3;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.insets = new Insets(4,4,4,4);
imagePane.setPreferredSize(new Dimension(720,810));
this.add(imagePane,constraints);

this.pack();

标签不在原始布局中。每个ScrollPanes最终都会包含一个JList,但是现在我只是想让空布局看起来正确,但事实并非如此。我现在得到的是这个。

What I actually get

文本字段不能说服占用3列,或者第一个滚动窗格占用3列。我已经尝试过以我能想到的方式改变它,但没有任何帮助。我可能会放弃这个并使用BoxLayout做嵌套面板,但我想知道为什么这不起作用,因为我认为我做的一切都是正确的。任何见解?提前谢谢。

1 个答案:

答案 0 :(得分:2)

Swing在这里感到困惑,因为它并不真正引用列的位置。

在这种情况下,最佳解决方案是创建至少一行包含所有列,并在这些列上定义weightx。

此外,JEditorPane在gridBagLayout中不能很好地工作。您应该使用JScrollPane(或其他东西)并将JEditorPane放在此窗格中。

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(1080, 720));

    this.setLayout(new GridBagLayout());
    GridBagConstraints constraints;

    JLabel a = new JLabel();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.weightx=0.25;
    constraints.insets = new Insets(4,4,4,4);
    this.add(a,constraints);

    JLabel b = new JLabel();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.weightx=0.25;
    this.add(b,constraints);

    JLabel c = new JLabel();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 2;
    constraints.gridy = 0;
    constraints.weightx=0.5;
    constraints.insets = new Insets(4,4,4,4);
    this.add(c,constraints);

    JLabel d = new JLabel();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 3;
    constraints.gridy = 0;
    constraints.weightx=0;
    this.add(d,constraints);

    JLabel p1 = new JLabel("Project - 1");
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth=2;
    constraints.insets = new Insets(4,4,4,4);
    this.add(p1,constraints);

    JLabel p2 = new JLabel("Project - 2");
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 2;
    constraints.gridy = 0;
    constraints.gridwidth=2;
    constraints.insets = new Insets(4,4,4,4);
    this.add(p2,constraints);


    JScrollPane projectScrollPane = new JScrollPane();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.weighty=1;
    constraints.insets = new Insets(4,4,4,4);
    this.add(projectScrollPane,constraints);

    JScrollPane fieldScrollPane = new JScrollPane();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.insets = new Insets(4,4,4,4);
    fieldScrollPane.setPreferredSize(new Dimension(540, 240));
    this.add(fieldScrollPane,constraints);

    JTextField searchTextField = new JTextField();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridwidth = 3;
    constraints.insets = new Insets(4,4,4,4);
    searchTextField.setPreferredSize(new Dimension(1000,30));
    this.add(searchTextField,constraints);

   JButton searchButton = new JButton("Search");
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.LINE_END;
    constraints.gridx = 3;
    constraints.gridy = 2;
    constraints.insets = new Insets(4,4,4,4);
    searchButton.setSize(new Dimension(80,30));
    this.add(searchButton,constraints);

    JScrollPane imageScrollPane = new JScrollPane();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.insets = new Insets(4,4,4,4);
    constraints.weighty=1;
    this.add(imageScrollPane,constraints);

    JScrollPane imagePane = new JScrollPane();
    constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 3;
    constraints.weighty=1;
    constraints.insets = new Insets(4,4,4,4);
    this.add(imagePane,constraints);

    this.pack();