首页 > 解决方案 > 如何在 SWT 中将背景图像设置为外壳或合成

问题描述

public class createShell{

        System.out.println("Inside shell create");
        display = new Display();
        shell = new Shell(display);
        shell.setSize(990, 590);
       
        shell.setLayout(new GridLayout());
        
        Composite comp = new Composite(shell, SWT.NO_FOCUS);
        comp.setBounds(10, 10, 720, 400);
        
}

我有这样的现有代码。需要给shell设置一个背景图片。如何给出图像的相对路径。插件(com.vcc.rac.ks5lmpp)的文件夹结构与图像相同。path代码文件在包里面的src文件夹(plugins\com.rac.ks5lmpp\src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\createShell.java) 有一个图片文件夹(plugins\com.rac.ks5lmpp \src\com\kjj\rac\ks5lmpp\stylesheets\dialogs\images),其中有我想作为背景传递的图像。图像大小为 676X324 的 PNG 类型。

提前致谢。

小路

标签: eclipse-pluginswt

解决方案


FileLocator在 Eclipse 插件中使用以在插件中查找资源。

Bundle bundle = FrameworkUtil.getBundle(getClass()); // Or some other way to find the current bundle

URL url = FileLocator.find(bundle, new Path("path within the plugin"));

ImageDescriptor desc = ImageDescriptor.createFromURL(url);

Image image = desc.createImage();

shell.setBackgroundImage(image);

注意:您应该安排在 shell 关闭时处理图像。

确保build.properties插件的 包含图像。

FileLocatororg.eclipse.core.runtime.FileLocator

Pathorg.eclipse.core.runtime.Path(不是 java.nio.file 路径)

ImageDescriptororg.eclipse.jface.resource.ImageDescriptor


推荐阅读