miglayout:无法正确对齐工作

Jas*_*n S 7 swing miglayout

有些东西不在这里.我试图让最右边的按钮(标有"帮助",在下面的例子中)被右对齐到JFrame,和硕大的按键有其宽度绑到JFrame但是要每人至少180像素.我得到了巨大的按钮约束,但没有正确的对齐.

替代文字

我认为正确的对齐是通过gapbefore push(如在其他SO问题中)完成的,但我无法弄明白.

谁能帮我?

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class RightAlignQuestion {
    public static void main(String[] args) {
        JFrame frame = new JFrame("right align question");
        JPanel mainPanel = new JPanel();
        frame.setContentPane(mainPanel);

        mainPanel.setLayout(new MigLayout("insets 0", "[grow]", ""));

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new MigLayout("", "[][][][]", ""));
        for (int i = 0; i < 3; ++i)
            topPanel.add(new JButton("button"+i), "");
        topPanel.add(new JButton("help"), "gapbefore push, wrap");
        topPanel.add(new JButton("big button"), "span 3, grow");

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new MigLayout("",
              "[180:180:,grow][180:180:,grow]","100:"));
        bottomPanel.add(new JButton("tweedledee"), "grow");
        bottomPanel.add(new JButton("tweedledum"), "grow");

        mainPanel.add(topPanel, "grow, wrap");
        mainPanel.add(bottomPanel, "grow");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jas*_*n S 8

没关系,我明白了:看起来在列规范中需要有一个间隙约束,而不是在组件级别:

    topPanel.setLayout(new MigLayout("", "[][][]push[]", ""));
Run Code Online (Sandbox Code Playgroud)

  • “ panel.add(button,“ cell 2 0,gapleft push”);`帮了我大忙。请记住,只有在添加组件时才能完成此操作。 (2认同)