0

我想使用 MiG Layout 实现本示例中的布局。所以最后一个 JButton 位于 JFrame 的底部。

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 布局中的行定义,所以我尝试了这段代码(和几个派生)

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");
}

但不幸的是,这不能按预期工作。

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

4

1 回答 1

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() 的第一个参数中的“fill”在许多情况下都很有用(或“filly”、“fillx”)。我认为你的 gaptop 给出了 0px 的差距。

于 2012-03-29T16:03:49.720 回答