java-需要按钮留在对话框的底部
作者:互联网
我最初将此问题发布在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");
标签:miglayout,swing,java,user-interface 来源: https://codeday.me/bug/20191101/1986234.html