Java Miglayout间隙推送不能按预期工作

时间:2012-03-29 07:13:30

标签: java swing layout-manager miglayout

我希望使用MiG Layout实现此示例中的布局。所以最后一个JButton位于JF​​rame的底部。

public class MainFrame extends JFrame {

    public static void main(String[] args) {
         MainFrame mainFrame = new MainFrame();
         mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
         mainFrame.setPreferredSize(new Dimension(400, 300));
         mainFrame.pack();
         mainFrame.setLocationRelativeTo(null);
         mainFrame.setVisible(true);
    }

    public MainFrame() {
        this.setLayout(new MigLayout("debug", "[grow, fill]", "[][][]push[]"));
        this.add(new JButton("Test1"), "wrap");
        this.add(new JButton("Test2"), "wrap");
        this.add(new JButton("Test..."), "wrap");
        this.add(new JButton("TestBottom"), "");
    }
}

我的问题是我要添加的JButton的数量是可变的,所以我不能在MiG Layout中使用行定义,所以我尝试了这段代码(和几个派生词)

public MainFrame() {
    this.setLayout(new MigLayout("debug", "[grow, fill]", ""));
    this.add(new JButton("Test1"), "wrap");
    this.add(new JButton("Test2"), "wrap");
    this.add(new JButton("Test..."), "wrap");
    this.add(new JButton("TestBottom"), "gaptop push");
}

但不幸的是,这并没有按照预期发挥作用。

有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为这看起来像你想要的。 (为你的工作榜样感到荣幸)

public MainFrame() {
    this.setLayout(new MigLayout("debug, filly", "[grow, fill]", ""));
    this.add(new JButton("Test1"), "wrap");
    this.add(new JButton("Test2"), "wrap");
    this.add(new JButton("Test..."), "wrap");
    this.add(new JButton("TestBottom"), "push, bottom");
}

我发现MigLayout()的第一个参数中的“填充”在许多情况下都很有用(或“filly”,“fillx”)。你认为你的缺口是一个0px的差距。