有些东西不在这里。我试图让最右边的按钮(标记为"帮助"在下面的示例中)与JFrame右对齐,以及巨大的按钮使其宽度与JFrame绑定但是在每个至少180px。我得到了巨大的按钮约束,但没有正确的对齐。
我认为正确的对齐是由gapbefore push
完成的(如在this other SO question中),但我无法弄清楚。
任何人都可以帮助我吗?
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);
}
}
答案 0 :(得分:8)
没关系,我明白了:看起来在列规范中需要有一个间隙约束,而不是在组件级别:
topPanel.setLayout(new MigLayout("", "[][][]push[]", ""));
答案 1 :(得分:3)
更简单/更清洁的方式(IMOH)正在使用组件约束并执行
topPanel.add(new JButton(“help”),“push,al right,wrap”);
当窗口伸展时,Push会将单元格推出,但您需要告诉组件将自身绑定到单元格的右侧。您可以使用以下代码实现上述目的。
JPanel topPanel = new JPanel();
frame.setContentPane(topPanel);
for (int i = 0; i < 3; ++i)
topPanel.add(new JButton("button"+i), "");
topPanel.add(new JButton("help"), "push, al right, wrap");
topPanel.add(new JButton("big button"), "span 3, grow, wrap");
topPanel.add(new JButton("tweedledee"), "span, split2,grow, w 180, h 100");
topPanel.add(new JButton("tweedledum"), "w 180, h 100, grow");