【问题标题】:How to Add Text and Image Both in a SWT LABEL如何在 SWT LABEL 中同时添加文本和图像
【发布时间】:2017-02-08 01:17:02
【问题描述】:

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

【问题讨论】:

    标签: eclipse-plugin swt eclipse-rcp


    【解决方案1】:

    不,您不能在 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();
    
        }
    }
    

    Output:

    【讨论】:

    • 我找到了,我必须将样式设置为 SWT.RIGHT_TO_LEFT :)
    • 除非非常必要,否则避免使用 CWidget。对于这个用例,并排放置 2 个标签(一个带有图像,另一个带有文本)要好得多。
    • @Mickael - 请告诉我们为什么不应该使用 CLabel 并使用您建议的更详细的方法。同样没有必要对答案进行投票,因为它充分满足了要求!
    • CWidget 不是由底层 UI 工具包(GTK、Win32...)绘制的,而是由 SWT 本身绘制的。它很容易导致与 UI 框架和其他 SWT 小部件的不一致。 bugs.eclipse.org/bugs/show_bug.cgi?id=501483
    • @Mickael 你提到的错误链接只是一个建议,并没有说明为什么不应该使用 CLabel。您还在那个 bug 中引用了 javadoc,提到了 CLabel 部署的不同策略。在没有它的情况下,开发人员应该自己编写,根据我的经验,如果做得不好,这更容易出错!此外,还有一个额外的平台支持问题,Eric 在他的 bug 链接的评论中以某种方式提到了这一点,例如,windows 不支持某些本机小部件的某些样式位。
    【解决方案2】:

    按钮单击打开文件对话框并选择任何图像以在特定标签上显示文本。

    import org.eclipse.swt.custom.CLabel    
    

    支持对齐文本和/或图像以及不同边框样式的标签。

    我希望这个答案是有用的。

    请访问此页面: How to load image to view in RCP?

    【讨论】:

    • 除非非常必要,否则避免使用 CWidget。对于这个用例,并排放置 2 个标签(一个带有图像,另一个带有文本)要好得多。
    • @Mickael 感谢您的提前.....Text 和 Image 都使用 Clabel 显示,不能正常工作 Label..不是两个标签的使用..你明白兄弟...
    • CWidget 不是由底层 UI 工具包(GTK、Win32...)绘制的,而是由 SWT 本身绘制的。它很容易导致与 UI 框架和其他 SWT 小部件的不一致。 bugs.eclipse.org/bugs/show_bug.cgi?id=501483
    【解决方案3】:

    是的,使用具有正确布局的中间组合

        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayout(new RowLayout(SWT.HORIZONTAL));
        Label imageLabel = new Label(composite, SWT.NONE);
        mageLabel.setImage(...);
        Label textLabel = new Label(composite, SWT.NONE);
        textLabel.setText(...)
    

    【讨论】:

    • 两个标签不考虑...标签中的文本和图像...不适合与问题相关的问题
    • @BharatZala 查看 cmets 关于为什么普通小部件(甚至 2 个标签)优于 CWidgets 的其他答案。
    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多