3

我最初在 miglayout 论坛上发布了这个问题,经过 534 次浏览但没有答案,我决定在那里尝试一下;-)

我试图从 MigLayout 白皮书中扩展“初始示例”,以便添加一个始终位于对话框底部的“确定”按钮。

不幸的是,我发现的唯一解决方案是添加一个会增长的“假面板”:

public class TestResize extends JDialog {
    protected JPanel contentPane;

    public TestResize() {
        super((Dialog) null, "Test resize", true);
        setupUI();
        setContentPane(contentPane);
    }

    private void setupUI() {
        contentPane = new JPanel(new MigLayout());
        contentPane.add(new JLabel("Enter size:"), "");
        contentPane.add(new JTextField(""), "grow, pushx, wrap");
        contentPane.add(new JLabel("Enter weight:"), "");
        contentPane.add(new JTextField(""), "grow, pushx, wrap");
        // fake panel that is allowed to grow
        contentPane.add(new JPanel(), "span 2, grow, pushy, wrap");
        JButton okButton = new JButton("Ok");
        JPanel buttonPanel = new JPanel(new MigLayout("", "[center, grow]"));
        buttonPanel.add(okButton, "");
        contentPane.add(buttonPanel, "dock south");
    }

    public static void main(String[] args) {
        TestResize dialog = new TestResize();
        dialog.pack();
        dialog.setVisible(true);
    }
}

我真的一点也不喜欢这种方法……但是有更好的方法吗?

(貌似我不能上传图片,但我想得到的用户界面在我的原始帖子中是可见的)

谢谢!

4

1 回答 1

4

如果您使用显式网格构造 MigLayout,则可以在两行之间添加“:push”:

new MigLayout(
      "",                 // Layout Constraints
      "[][]",             // Column constraints
      "[][][]:push[]");   // Row constraints

(参见备忘单的“列/行约束”部分)

编辑:

实际上,更好的解决方案是在上一行的末尾使用“wrap push”。然后您不需要显式设置网格中的行数:

contentPane.add(new JPanel(), "span 2, grow, pushy, wrap push");
于 2012-01-22T03:13:00.607 回答