2017-08-25 21 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. 

注意:如果您沒有指定插圖,按鈕將是約。中心,但不完全在中心。調試參數可幫助您查看單元的大小。你可以簡單地省略它。

相關問題