gpt4 book ai didi

java - MigLayout 和垂直居中的组件

转载 作者:搜寻专家 更新时间:2023-11-01 02:25:44 27 4
gpt4 key购买 nike

我想让按钮居中,如下所示:

expected result

这是我的代码:

import java.awt.Dimension;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class MigLayoutTest extends JFrame {

public static void main(String[] args) {

JPanel content = new JPanel();
content.setLayout(new MigLayout("center, wrap, gapy 20"));

JButton buttonA = new JButton("button A");
buttonA.setPreferredSize(new Dimension(100,30));
JButton buttonB = new JButton("button B");
buttonB.setPreferredSize(new Dimension(80,80));
JButton buttonC = new JButton("button C");
buttonC.setPreferredSize(new Dimension(300,40));
JButton buttonD = new JButton("button D");
buttonD.setPreferredSize(new Dimension(200,60));

content.add(buttonA);
content.add(buttonB);
content.add(buttonC);
content.add(buttonD);

JFrame frame = new JFrame("MigLayout Test");
frame.setContentPane(content);
frame.setSize(600, 400);
frame.setVisible(true);
}

}

按钮居中,但不是垂直的。

有什么建议吗?提前致谢。

最佳答案

白皮书定义了确切的语法:

al/align alignx [aligny]

继续:

The alignment can be specified as a UnitValue or AlignKeyword.

因此,为了使整个 block 沿两个轴居中,使用 AlignKeyword 您需要两个参数:

new MigLayout("al center center, wrap, gapy 20"); // centers in both directions

下一句:

If an AlignKeyword is used the "align" keyword can be omitted.

这将是:

new MigLayout("center center, wrap, gapy 20"); // centers horizontally only

虽然不起作用,但在解析参数时看起来有点小故障。

关于java - MigLayout 和垂直居中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com