如何在 SWT LABEL 中同时添加文本和图像 [英] How to Add Text and Image Both in a SWT LABEL

查看:22
本文介绍了如何在 SWT LABEL 中同时添加文本和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在单行中添加 SWT 标签中的文本和图像.添加图片后,文字就会消失.

Is there any way to add Text and Image in SWT label in a single line. Once I add image, text goes off.

推荐答案

不,您不能在 Label 中同时包含图像和文本(除非您自定义绘制它).否则使用 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:

代码:

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();

    }
}

输出:

这篇关于如何在 SWT LABEL 中同时添加文本和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆