我最初在 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);
}
}
我真的一点都不喜欢这种方法...但是有更好的方法吗?
(貌似不允许上传图片,但是我要获取的UI是可见的in my original post)
谢谢!
最佳答案
如果您使用显式网格构造 MigLayout,则可以在两行之间添加“:push”:
new MigLayout(
"", // Layout Constraints
"[][]", // Column constraints
"[][][]:push[]"); // Row constraints
(参见 cheatsheet 的“列/行约束”部分)
编辑:
实际上,更好的解决方案是在上一行的末尾使用“wrap push”。然后你不需要显式设置网格中的行数:
contentPane.add(new JPanel(), "span 2, grow, pushy, wrap push");
关于java - 需要按钮留在我的对话框底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957372/