Java 如何使用MiGLayout在包含多个组件的行上居中放置组件

Java 如何使用MiGLayout在包含多个组件的行上居中放置组件,java,swing,miglayout,Java,Swing,Miglayout,大约一个半月前,我开始使用MiGLayout,一切都很简单,效果很好。只有一个问题我还没有解决 假设我想有一行,在最右边有两个按钮和一个居中的标题,当我这样做时,标题实际上不会居中: (“这”是一个JPanel) 实际情况是,“labelTitle”位于按钮放置后剩余可用空间的中心,但我实际上希望它相对于整个JPanel居中,而不仅仅是剩余空间 我可以使用哪些参数来获得所需的效果?我知道我可以使用绝对定位,但我不想这样做,因为在我的情况下,它首先违背了使用MiGLayout的目的。您可以使用JX

大约一个半月前,我开始使用MiGLayout,一切都很简单,效果很好。只有一个问题我还没有解决

假设我想有一行,在最右边有两个按钮和一个居中的标题,当我这样做时,标题实际上不会居中:

(“这”是一个JPanel)

实际情况是,“labelTitle”位于按钮放置后剩余可用空间的中心,但我实际上希望它相对于整个JPanel居中,而不仅仅是剩余空间


我可以使用哪些参数来获得所需的效果?我知道我可以使用绝对定位,但我不想这样做,因为在我的情况下,它首先违背了使用MiGLayout的目的。

您可以使用JXLayer并将按钮放在玻璃窗格中

JButton closeButton = new JButton("Close");
JButton mainMenuButton = new JButton("Menu");
JLabel labelTitle = new JLabel("Application");

JPanel panel = new JPanel();
panel.setLayout(new MigLayout(new LC().fillX()));
panel.add(labelTitle, new CC().alignX("center").spanX());

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new MigLayout(new LC().fillX()));
buttonPanel.add(closeButton, new CC().alignX("right").split());
buttonPanel.add(mainMenuButton, new CC().alignX("right"));
buttonPanel.setOpaque(false);

JXLayer<JPanel> mainPanel = new JXLayer<JPanel>();
mainPanel.setView(panel);
mainPanel.setGlassPane(buttonPanel);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.setSize(400, 600);
frame.setVisible(true); 
JButton closeButton=新JButton(“Close”);
JButton mainMenuButton=新JButton(“菜单”);
JLabel labelTitle=新的JLabel(“应用”);
JPanel面板=新的JPanel();
panel.setLayout(新的migloayout(新的LC().fillX());
panel.add(labelTitle,新CC().alignX(“中心”).spanX());
JPanel buttonPanel=新的JPanel();
buttonPanel.setLayout(新的MigLayout(新的LC().fillX());
buttonPanel.add(closeButton,新CC().alignX(“右”).split());
添加(主菜单按钮,新建CC().alignX(“右”);
buttonPanel.set不透明(假);
JXLayer主面板=新建JXLayer();
主面板。设置视图(面板);
主面板。设置玻璃面板(按钮面板);
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(主面板);
框架。设置尺寸(400600);
frame.setVisible(true);

创建JPanel时,请使用以下MigLayout初始值设定项:
new MigLayout(“,”[]按[中心]按[]),”)

如果您不了解约束,请检查此处:


这是假设您在这个JPanel中没有任何其他东西…

您会寻找这样的东西吗

干杯

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

    JPanel panel = new JPanel(new MigLayout("debug"));
    panel.add(new JLabel("Label Title"), "x2 min(b1.x - unrel, (container.w+pref)/2)");
    panel.add(new JButton("Close Button"), "id b1, pushx, alignx right");
    panel.add(new JButton("Main Menu Button"), "alignx right");

    frame.add(panel);
    frame.setSize(800, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

谢谢你的尝试,但它不起作用,它仍然在剩余的空间中居中,这不是面板的中心。是的,但它不起作用,我不知道为什么@Mikael Grev给了我一个有效的解决方案,谢谢你的尝试!它工作完美无瑕。不过,我对布局的了解还不足以理解所有参数。想解释一下标题标签是如何工作的吗?我真的很感谢你的帮助!这很容易。它只是为标签的右侧(x2)设置一个表达式。在表达式中是左按钮的x,为了能够引用它,我将ID设置为b1。我非常确定它会起作用,但我正在寻找一个带有MiGLayout的解决方案,无论如何,谢谢!
public static void main(String[] args)
{
    JFrame frame = new JFrame();

    JPanel panel = new JPanel(new MigLayout("debug"));
    panel.add(new JLabel("Label Title"), "x2 min(b1.x - unrel, (container.w+pref)/2)");
    panel.add(new JButton("Close Button"), "id b1, pushx, alignx right");
    panel.add(new JButton("Main Menu Button"), "alignx right");

    frame.add(panel);
    frame.setSize(800, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
}