MigLayout面板内的MigLayout面板 - 将其对齐到底部

时间:2016-01-20 20:49:33

标签: java swing user-interface miglayout

enter image description here

右边的面板上有所有按钮,我想对齐底部。

JPanel easternDock = new JPanel(new MigLayout("", ""));
easternDock.add(button1, "wrap");
....
this.add(easternDock);

我认为我可以在所有按钮上方添加一个组件并使其在y维度上增长以填充屏幕,但我不确定我将使用哪个组件以及我无法找到任何旨在做这种事情的组件。

1 个答案:

答案 0 :(得分:3)

我这样做的方法是在“eastDock”面板中包含另一个面板,其中包含所有组件,并使用“推送列/行”约束将“eastDock”推送到底部。

来自MiG Cheat表:http://www.miglayout.com/cheatsheet.html

  

“:push”(或“push”,如果与默认间隙大小一起使用)可以添加到间隙大小以使该间隙贪婪并尝试占用尽可能多的空间而不使布局大于容器。

以下是一个例子:

public class AlignToBottom {

public static void main(String[] args) {
    JFrame frame = new JFrame();

    // Settings for the Frame
    frame.setSize(400, 400);
    frame.setLayout(new MigLayout(""));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Parent panel which contains the panel to be docked east
    JPanel parentPanel = new JPanel(new MigLayout("", "[grow]", "[grow]"));

    // This is the panel which is docked east, it contains the panel (bottomPanel) with all the components
    // debug outlines the component (blue) , the cell (red) and the components within it (blue)
    JPanel easternDock = new JPanel(new MigLayout("debug, insets 0", "", "push[]")); 

    // Panel that contains all the components
    JPanel bottomPanel = new JPanel(new MigLayout());


    bottomPanel.add(new JButton("Button 1"), "wrap");
    bottomPanel.add(new JButton("Button 2"), "wrap");
    bottomPanel.add(new JButton("Button 3"), "wrap");

    easternDock.add(bottomPanel, "");

    parentPanel.add(easternDock, "east");

    frame.add(parentPanel, "push, grow");
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

}