SWT使用Label排列ICON_WARNING

时间:2013-04-23 16:22:16

标签: java eclipse swt

我想在选中某个复选框时显示一个复合到Eclipse。复合材料将由两部分组成:WARNING_ICON和带有一些文本的标签。

我似乎无法让布局完全正确 - 我想并排显示图像和标签。这是我的代码部分:

        final Composite warningComposite = new Composite(parent, SWT.NONE);
        warningComposite.setLayout(new GridLayout(2, false));
        warningComposite.setLayoutData(new GridData(0, 0, true, false));

        Label l = SWTUtilities.createLabel(composite, "", -1, -1, 1, GridData.BEGINNING);
        final Image img = PlatformUI.getWorkbench().getDisplay().getSystemImage(SWT.ICON_WARNING);
        l.setImage(img);
        l.setLayoutData(new GridData());

        l = SWTUtilities.createLabel(composite, "Wizards Gone Wild", -1, -1, 1, GridData.END);

SWTUtilities.createLabel方法:

public static Label createLabel(final Composite parent, final String label, final int width, final int indent, final int span, final int style) {
        Assert.isNotNull(parent);
        Assert.isNotNull(label);
        final Label control = new Label(parent, SWT.WRAP);
        control.setText(label);
        if (width > 0 || indent > 0 || span > 0) {
            final GridData data = new GridData(style);
            if (width > 0)
                data.widthHint = width;
            if (indent > 0)
                data.verticalIndent = indent;
            if (span > 1)
                data.horizontalSpan = span;
            control.setLayoutData(data);
        }
        return control;
    }

1 个答案:

答案 0 :(得分:2)

你去了:

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

    Label image = new Label(shell, SWT.NONE);
    image.setImage(d.getSystemImage(SWT.ICON_WARNING));
    image.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Label text = new Label(shell, SWT.NONE);
    text.setText("SOME TEXT HERE");
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));


    shell.pack();
    shell.open();
    while (!shell.isDisposed())
        while (!d.readAndDispatch())
            d.sleep();
}

看起来像这样:

enter image description here


以下是解释:SWT不支持Label内文本的垂直对齐(因为并非所有操作系统都支持它)。解决方案是将标签对齐在父级的中心,而不是标签中的文本。