【问题标题】:Creating Button with Image in eclipse not working在 Eclipse 中创建带有图像的按钮不起作用
【发布时间】:2014-11-21 15:41:45
【问题描述】:

我正在尝试创建一个带有图像的按钮。该 API 可以在 here 找到。

这是我的代码:

Button settings = new Button(swpContainer, SWT.PUSH);
settings.setText("Settings");
settings.setImage(new Image(null, "/myProject/icons/settings.png"));

这是我得到的例外..

org.eclipse.swt.SWTException: i/o error (java.io.FileNotFoundException:    \myProject\icons\settings.png (The system cannot find the path specified))

我在 Eclipse 中右键单击图像并从属性中获取路径。 任何帮助将不胜感激!

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    试试这个:

    Button settings = new Button(swpContainer, SWT.PUSH);
    settings.setText("Settings");
    Image image = new Image(swpContainer.getShell().getDisplay(), 
              getClass().getClassLoader().getResourceAsStream("icons/settings.png")); //$NON-NLS-1$
    settings.setImage(image);
    

    【讨论】:

      【解决方案2】:

      假设myProject是你的项目名称,icons是项目中的一个简单文件夹,那么new Image(null, "icons/settings.png");就足够了。

      我也推荐:

      【讨论】:

        【解决方案3】:

        如果您正在为 RCP 应用程序编写它,请尝试使用 ImageDescriptor 并创建图像。

        Image IMG_EXAMPLE = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
              "/icons/settings.png").createImage();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-19
          • 2014-08-04
          • 1970-01-01
          • 2019-11-11
          • 2019-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多