【问题标题】:Limit growing of column with MigLayout使用 MigLayout 限制列的增长
【发布时间】:2018-08-01 00:31:13
【问题描述】:

我有以下测试Shell,使用MigLayout 我想创建一个简单的表单(左标签,右文本字段)。
标签应占用所需的空间,并且文本应自动增长到父复合材料的右边缘。

在我将大文本添加到文本字段之前,这很有效。文本字段然后增长到父复合的边缘:

短文本有效

长文本超出边缘

如果文本太长,如何配置右列以在右边缘停止增长?

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import net.miginfocom.swt.MigLayout;

public class TestShell {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setSize(400, 200);
        shell.setLayout(new GridLayout());

        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setLayoutData(new GridData(200, 100));

        composite.setLayout(new MigLayout("fillx"));

        Label label = new Label(composite, SWT.NONE);
        label.setText("LABEL");

        Text text = new Text(composite, SWT.BORDER);
        text.setLayoutData("growx, pushx");
        //      text.setText("TEXT");
        text.setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}


使用 GridLayout 可以正常工作:

composite.setLayout(new GridLayout(2, false));
...
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));

有没有一种方法可以让MigLayout 表现得像GridLayout 但具有所有额外的MigLayout 功能?

【问题讨论】:

    标签: java swt miglayout


    【解决方案1】:

    最简单的方法是在shell.open();之后调用setText

    ...
    shell.open();
    text.setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 2012-12-20
      • 2016-04-27
      • 2010-12-07
      • 2010-11-12
      • 2013-06-23
      • 1970-01-01
      • 2016-12-04
      相关资源
      最近更新 更多