【问题标题】:Make a column of a GridLayout fill horizontal使 GridLayout 的一列水平填充
【发布时间】:2014-10-09 22:20:31
【问题描述】:

有一个给定的公式,例如您在图像中看到的那个。 我正在为 swt、RCP-App 编程。 我无法直接访问组件,但必须使用布局对齐它们。 首先,使用 Gridlayout 来对齐组件。 第一列包含标签,第二列包含文本字段。

剩下的是,我希望文本字段具有相同的宽度。 我最初的计划是让文本字段水平填充第二列,然后做某事。比如margin-right。

GridData 有以下功能:

GridData data = new GridData(SWT.FILL, SWT.NONE, true, false);
data.horizontalAlignment = GridData.FILL;
...

我的 GridLayout 代码如下所示:

GridLayout layout = new Gridlayout(2, false);

GridData data = new GridData(SWT.FILL, SWT.NONE, true, false);
data.horizontalAlignment = GridData.FILL;

parent.setLayout(layout); -> Produces the given image
parent.setLayoutData(data);
-> Seems to do nothing in combination with gridlayout, but the layout is needed

有更多不同宽度的字段,但这应该足以解释问题。

有没有人知道如何将布局与网格数据结合起来或有其他解决方案。 我想让第二列中的文本字段更漂亮,而无需直接触摸它们。

看起来是这样的:

感谢您的帮助

【问题讨论】:

  • 可以添加图片链接。
  • 谢谢,我刚刚添加了图片。
  • 更新了我的答案以满足您的要求。

标签: java alignment swt grid-layout


【解决方案1】:

GridData 必须应用于Texts,而不是它们的父级。所以你必须“触摸”它们:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    new Label(shell, SWT.NONE).setText("name");
    Text name = new Text(shell, SWT.BORDER);
    name.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    new Label(shell, SWT.NONE).setText("memory");
    Text memory = new Text(shell, SWT.BORDER);
    memory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    new Label(shell, SWT.NONE).setText("processors");
    Text processors = new Text(shell, SWT.BORDER);
    processors.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    shell.pack();
    shell.open();

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

看起来像这样:


更新

好的,再次考虑您的要求,并提出了另一个解决方案。在此解决方案中,您不必引用实际的 Text 对象,但您将搜索它们:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    new Label(shell, SWT.NONE).setText("name");
    new Text(shell, SWT.BORDER);

    new Label(shell, SWT.NONE).setText("memory");
    new Text(shell, SWT.BORDER);

    new Label(shell, SWT.NONE).setText("processors");
    new Text(shell, SWT.BORDER);

    applyGridData(shell);

    shell.pack();
    shell.open();

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

private static void applyGridData(Control parent)
{
    if(parent instanceof Text)
    {
        parent.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    }
    else if(parent instanceof Composite)
    {
        Composite comp = (Composite) parent;

        for(Control control : comp.getChildren())
            applyGridData(control);
    }
}

基本上,此解决方案在您的Composite 中递归搜索Text 对象,然后将GridData 应用于它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多