ITeye博客

工具栏图片资源的管理

visionary_2006 · 2007-08-03 14:34 · RCP · Eclipse · 配置管理 · 软件测试 · Markdown

如果软件开发界面中的工具栏中只有一两个图片,这种直接创建图片对象然后释放的办法是可行的,但当图片增多时也要一个一个地创建和释放吗?可想而知,代码将变得复杂,所以有必要将图片的管理集中到一起,进行统一的管理.

main方法:

package sxjm;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import swtDemo.ImageFactory;

public class ToolBarSample ...{
    public static void main(String args[])
    ...{
        Display d = new Display();
        Shell s = new Shell(d);
        s.setLayout(new GridLayout());
        Composite tool  =  new Composite(s,SWT.NONE);
        tool.setLayoutData(new GridData(SWT.LEFT,SWT.TOP,true,false));
        ToolBar toolBar = new ToolBar(tool,SWT.NONE);
        ToolItem saveItem = new ToolItem(toolBar,SWT.PUSH);
        saveItem.setImage(ImageFactory.loadImage(d,ImageFactory.SAVE_EDIT));
        saveItem.setText("save");
        toolBar.pack();
        s.setSize(new Point(200,200));
        s.open();
        while(!s.isDisposed())
        ...{
            if(!d.readAndDispatch())
            ...{
                d.sleep();
            }
        }
        ImageFactory.dispose();
        d.dispose();
        
    }
}

下面是

ImageFactory类

package swtDemo;
import java.util.Enumeration;
import java.util.Hashtable;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class ImageFactory ...{
    private ImageFactory()...{}
        //配置图片的绝对路径
        public static  final String REAL_PATH = "E:\workspace\icons\";
        //一些图片名称的常量
        public static final String DELETE_EDIT="delete_edit.jpg";
        public static final String SAVE_EDIT="save_edit.jpg";
        public static final String COPY_EDIT="copy_edit.jpg";
        public static final String PRINT_EDIT="print_edit.jpg";
        //..........
        
        private static  Hashtable htImage = new Hashtable();
        
        public static Image loadImage(Display display , String imageName)
        ...{
            Image image = (Image)htImage.get(imageName.toUpperCase());
            if(image==null)
            ...{
                image = new Image(display,REAL_PATH+imageName);
                htImage.put(imageName.toUpperCase(),image);
            }
            return image;
        }
        public static void dispose()
        ...{
            Enumeration e = htImage.elements();
            while(e.hasMoreElements())
            ...{
                Image image = (Image)e.nextElement();
                if(!image.isDisposed())
                ...{
                    image.dispose();
                }
            }
        }
}

如果显示不出,检查图片地址对否

下面这个示例创建了一个数字的哈希表。

//它将数字的名称用作键:

Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
 numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));

//要检索一个数字,可以使用以下代码:
Integer n = (Integer)numbers.get("two");
 if (n != null) ...{
         System.out.println("two = " + n);
 }

value.put(K key, V value)
将指定 key 映射到此哈希表中的指定
get(Object key)
返回此哈希表中指定键所映射到的值。

//实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。 

//例如,要输出向量 v 的所有元素,可使用以下方法: 

     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) ...{
         System.out.println(e.nextElement());
     }
//这些方法主要通过向量的元素、哈希表的键以及哈希表中的值进行枚举。枚举也用于将输入流指定到 SequenceInputStream 中。 

hasMoreElements()
测试此枚举是否包含更多的元素。
nextElement()
如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。