如何在SWT标签中添加文本和图像

Abh*_*ary 6 swt eclipse-plugin eclipse-rcp

有没有办法在一行中添加SWT标签中的文本和图像.一旦我添加图像,文本就会消失.

Fav*_*ius 16

不,你不能同时拥有一个图像和文本Label(除非你自定义绘制它).其他用途org.eclipse.swt.custom.CLabel:

Code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class LabelTest {
    public static void main(String[] args) 
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        Image image = new Image(display, "next.png");

        CLabel label = new CLabel(shell, SWT.BORDER);
        label.setImage(image);
        label.setText("This is a CLabel !!");

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


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

    }
}
Run Code Online (Sandbox Code Playgroud)

Output:

在此输入图像描述

  • 我找到了,我必须将样式设置为SWT.RIGHT_TO_LEFT :) (3认同)
  • 除非非常必要,否则请避免使用CWidgets.对于这个用例,并排放置2个标签(一个带有图像,另一个带有文本)要好得多. (2认同)