【问题标题】:Eclipse Plugin Set Image icon for a custom dialogEclipse 插件设置自定义对话框的图像图标
【发布时间】:2017-01-06 10:16:31
【问题描述】:

我制作了一个自定义 Eclipse 插件,它使用并显示多个对话框,我想知道是否可以将左上角的图标图像设置为我在插件的 icons 文件夹中使用的那个。我想获取该图标并设置它,而不是 Eclipse 使用的默认图标。 我正在重写configureShell() 方法来更改对话框标题,我还想更改图标。

@Override
protected void configureShell(Shell parent){
    super.configureShell(parent);
    parent.setText("Choose variant...");
    Image icon = new Image(parent.getDisplay(), "icons/best.gif"); - this method does not work as it cannot find the file
    parent.setImage(icon);
}

我也尝试使用getClass().getResource("best.gif") 并将图像放在同一个包中,但仍然找不到我提供的位置(FileNotFoundException),而且Image 构造函数不接受 URL 对象。

@Override
protected void configureShell(Shell parent){
    super.configureShell(parent);
    parent.setText("Choose variant...");
    Image icon = new Image(parent.getDisplay(), getClass().getResource("icons/best.gif"));
    parent.setImage(icon);
}

有没有办法使用我的 Eclipse 插件中已有的图标? 主要问题是从插件的icons 文件夹中获取图标并将其设为Image 对象。
谢谢。

【问题讨论】:

    标签: java eclipse plugins


    【解决方案1】:

    您可以像这样在您的插件activator class 中注册图标:

    @Override
    protected void initializeImageRegistry(final ImageRegistry reg) {
        reg.put(IMAGE_PATH, imageDescriptorFromPlugin(PLUGIN_ID, IMAGE_PATH));
    }
    

    图片路径是相对于你的插件的,例如icons/icon.png.

    您也可以通过激活器类访问这些图像:

    final Image image = MyActivatorClass.getDefault().getImageRegistry().get(IMAGE_PATH);
    myShell.setImage(image);
    

    (请注意,我使用图像路径作为图像注册表中的键,您不必这样做,但只需使用相同的静态 String 就可以使一切变得不那么复杂。)

    【讨论】:

    • 是的,它成功了,谢谢。我也可以注册多个图像?
    • 是的,你可以。也看看 gregs 的回答,你的激活器类必须扩展 AbstractUIPlugin 我错过了那一步。
    【解决方案2】:

    对于 Eclipse 插件,您可以使用 FileLocator 类在插件中查找资源。

    对于图像,请使用以下内容:

    String path = "icons/best.gif";
    
    URL url = FileLocator.find(bundle, new Path(path), null);
    
    ImageDescriptor desc = ImageDescriptor.createFromURL(url);
    
    Image image = desc.createImage();
    

    注意:您必须安排在不再需要时处理图像。

    如果您的激活器扩展了AbstractUIPlugin,您也可以使用其中的ImageRegistry。注册表将处理处置。

    确保icons 目录列在build.properties 文件中。当您导出插件时,缺少这将导致问题。

    【讨论】:

    • 它与ActivatorImage Registry 一起工作。也感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多