2017-08-25 65 views
0

是否有任何简单的方法将按钮居中置于一行上,同时按钮左侧的加载指示器不移动按钮本身?使用MigLayout将一个组件居中在包含另一个组件的行上

我希望按钮始终居中,并且加载指示器(JLabel)应该位于按钮的正面。

This solution seems way too complicated and doesn't actually work for what I want to do.

我到目前为止是这样的:

setLayout(new MigLayout("align center center")); 

add(_loadingIndicator, "center, split 2"); 
add(_applyButton, "center"); 

但中心2个部件在一起,使按钮从来没有真正的中心。

回答

0

让您的按钮完全位于中心的一种方法是让您的布局具有3列。您将按钮放置在中心位置,并将加载指示器放在右侧。 假设加载指示符是32x32像素。

setLayout(new MigLayout("debug", "[grow]32px[]0[grow]")); // The [][][]s are columns, meaning 3 columns. The numbers between them are the insets. 

add(_applyButton, "cell 1 0"); // Place the button in 2nd column, 1st row. 
add(_loadingIndicator, "cell 2 0"); // Place the indicator in 3rd column, 1st row. 

注意:如果您没有指定插图,按钮将是约。中心,但不完全在中心。调试参数可帮助您查看单元的大小。你可以简单地省略它。

相关问题