【问题标题】:Centering a component on a row which contain another component with MigLayout使用 MigLayout 将组件居中在包含另一个组件的行上
【发布时间】:2018-02-03 15:53:46
【问题描述】:

有没有什么简单的方法可以在不移动按钮本身的情况下使按钮居中并在按钮左侧显示加载指示器?

我希望按钮始终居中,并且加载指示符 (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 个组件居中,所以按钮永远不会真正居中。

【问题讨论】:

    标签: java miglayout


    【解决方案1】:

    让您的按钮完全位于中心的一种方法是让您的布局有 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.
    

    注意:如果您不指定插图,则该按钮将是大约。居中,但不完全居中。调试参数可帮助您查看单元格的大小。你可以直接省略它。

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2014-01-18
      相关资源
      最近更新 更多