【问题标题】:Add custom image to Eclipse SWT将自定义图像添加到 Eclipse SWT
【发布时间】:2014-05-31 00:04:10
【问题描述】:

如何将自定义图像添加到 Eclipse SWT,我尝试使用 ImageDescriptor。但我可能做错了什么。

private Action stopAction;
stopAction.setText("Stop");
        stopAction.setToolTipText("Stop running");


        stopAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
                getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

        ImageDescriptor imgDesc = null;
        try {
            imgDesc = ImageDescriptor.createFromURL(new URL("icons/stop.png"));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stopAction.setImageDescriptor(imgDesc);

【问题讨论】:

  • 你有异常吗?结果是什么?期望的结果是什么?
  • @Baz 异常是这样的:java.net.MalformedURLException: no protocol: icons/stop.png 期望的结果是获取自定义图像到按钮,所以使用这个 stopAction.setImageDescriptor(PlatformUI.getWorkbench() .getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));我使用下面的代码在图标中加载位于同一项目中的自定义图像

标签: java image url eclipse-plugin swt


【解决方案1】:

要从插件中的文件加载图像描述符,请使用:

final URL fullPathString = FileLocator.find(bundle, new Path("icons/stop.png"), null);

ImageDescriptor imgDesc = ImageDescriptor.createFromURL(fullPathString);

bundle 是您的插件包,请从您的 Activator 获取它或:

Bundle bundle = Platform.getBundle("plugin id");

不要忘记将您的 icons 文件夹添加到 build.properties 文件中,以便它包含在构建的插件中。

【讨论】:

  • 谢谢,但插件 id 是什么?我不能通过一些方法调用得到它吗?
  • 这是您在 MANIFEST.MF 中为插件定义的 id。在 MANIFEST.MF/plugin.xml 编辑器中,它是概览页面上的 ID 字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多