在需要使用图标的地方,需要这样写:
java 代码
- private static Image fIconFirefox = JSPlugin.getImage("icons/firefox_icon.png");
JSPlugin中取得照片的相关代码要这样写:
java 代码
- /**
- * Returns an image descriptor for the image file at the given
- * plug-in relative path.
- *
- * @param path the path
- * @return the image descriptor
- */
- public static ImageDescriptor getImageDescriptor(String path) {
- return AbstractUIPlugin.imageDescriptorFromPlugin("com.bstek.ide.js.editor", path); //$NON-NLS-1$
- }
- private static Hashtable images = new Hashtable();
- /**
- * getImage
- *
- * @param path
- * @return Image
- */
- public static Image getImage(String path)
- {
- if (images.get(path) == null)
- {
- ImageDescriptor id = getImageDescriptor(path);
- if (id == null)
- {
- return null;
- }
- Image i = id.createImage();
- images.put(path, i);
- return i;
- }
- else
- {
- return (Image) images.get(path);
- }
- }
最终读取图标对象在AbstractUIPlugin.imageDescriptorFromPlugin()方法中实现
java 代码
- /**
- * Creates and returns a new image descriptor for an image file located
- * within the specified plug-in.
- * <p>
- * This is a convenience method that simply locates the image file in
- * within the plug-in (no image registries are involved). The path is
- * relative to the root of the plug-in, and takes into account files
- * coming from plug-in fragments. The path may include $arg$ elements.
- * However, the path must not have a leading "." or path separator.
- * Clients should use a path like "icons/mysample.gif" rather than
- * "./icons/mysample.gif" or "/icons/mysample.gif".
- * </p>
- *
- * @param pluginId the id of the plug-in containing the image file;
- * <code>null</code> is returned if the plug-in does not exist
- * @param imageFilePath the relative path of the image file, relative to the
- * root of the plug-in; the path must be legal
- * @return an image descriptor, or <code>null</code> if no image
- * could be found
- * @since 3.0
- */
- public static ImageDescriptor imageDescriptorFromPlugin(String pluginId,
- String imageFilePath) {
- if (pluginId == null || imageFilePath == null) {
- throw new IllegalArgumentException();
- }
- // if the bundle is not ready then there is no image
- Bundle bundle = Platform.getBundle(pluginId);
- if (!BundleUtility.isReady(bundle)) {
- return null;
- }
- // look for the image (this will check both the plugin and fragment folders
- URL fullPathString = BundleUtility.find(bundle, imageFilePath);
- if (fullPathString == null) {
- try {
- fullPathString = new URL(imageFilePath);
- } catch (MalformedURLException e) {
- return null;
- }
- }
- if (fullPathString == null) {
- return null;
- }
- return ImageDescriptor.createFromURL(fullPathString);
- }
安徽新华电脑学校专业职业规划师为你提供更多帮助【在线咨询】