在包含多个组件的行上居中组件

在包含多个组件的行上居中组件

本文介绍了如何使用 MiGLayout 在包含多个组件的行上居中组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一个半月前我开始使用 MiGLayout,一切都很简单,而且效果很好.只有一个我仍然无法解决的问题.

I started using MiGLayout about a month and half ago and everything is simple and works great. There's only one issue I still have that I haven't been able to fix.

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

Let's say I want to have a row that has two buttons on the right-most side and a centered title, the title doesn't actually get centered when I do it this way:

("this" 是一个 JPanel)

("this" is a JPanel)

this.add(labelTitle, "split, span, center");
this.add(closeButton, "east");
this.add(mainMenuButton, "east");

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

What happens is that "labelTitle" is centered in the remaining space available after the buttons are placed, but I actually want it to be centered relative to the whole JPanel, not just the remaining space.

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

What parameters could I use to get the desired effect? I know I could use absolute positioning, but I don't want to do that because it defeats the purpose of using MiGLayout in the first place in my case.

推荐答案

这就是您要找的东西吗?

Can it be something like this you are looking for?

干杯!

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);
}

这篇关于如何使用 MiGLayout 在包含多个组件的行上居中组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:10