在代码或GUI设计器中设置布局对JPanel不起作用。布局总是FlowLayout

时间:2015-08-18 16:11:55

标签: java swing intellij-idea

我使用IntelliJ GUI Designer创建JPanel。它有三个垂直堆叠的JButtons。当我运行代码时,它有三个JButtons并排,就像使用FlowLayout一样。 FlowLayout是JPanel的默认布局。我已经尝试在GUI Designer和代码中将布局管理器设置为各种布局。它总是看起来像FlowLayout。如何使用JPanel的其他布局?我想使用设计器的GridLayoutManager。

这是java代码:

public class myJPanelTest extends JPanel
{
    private JButton button1;
    private JButton button2;
    private JButton button3;

    public myJPanelTest()
    {
        setLayout(new GridBagLayout());

        add(button1);
        add(button2);
        add(button3);
    }  
}

以下是表格:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="abc.dfg.tbs.CM.myJPanelTest">
  <grid id="27dc6" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="0" left="0" bottom="0" right="0"/>
    <constraints>
      <xy x="20" y="20" width="500" height="400"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <component id="c8918" class="javax.swing.JButton" binding="button1" default-binding="true">
        <constraints>
          <grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Button 1"/>
        </properties>
      </component>
      <vspacer id="8e9eb">
        <constraints>
          <grid row="1" column="1" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
        </constraints>
      </vspacer>
      <component id="dff25" class="javax.swing.JButton" binding="button2" default-binding="true">
        <constraints>
          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Button 2"/>
        </properties>
      </component>
      <component id="ac93b" class="javax.swing.JButton" binding="button3" default-binding="true">
        <constraints>
          <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
        </constraints>
        <properties>
          <text value="Button 3"/>
        </properties>
      </component>
    </children>
  </grid>
</form>

如何仅针对JPanel使用FlowLayout停止布局?

0 个答案:

没有答案