且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-05 16:06:22

不,您不能同时拥有图像和文本一个标签(除非你自定义绘制)。否则使用 org.eclipse.swt.custom.CLabel

No you can't have an image and text simultaneously in a Label (unless you custom draw it). Else use 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();

    }
}

输出:

Output: