---
title: "工具栏图片资源的管理"
author: "visionary_2006"
date: "2007-08-03T14:34:00Z"
tags: ["Eclipse", "软件测试", "配置管理"]
slug: "untitled"
draft: false
lastmod: "2007-08-03T14:34:00Z"
id: 158456
url: "https://opas.iteye.com/blog/158456"
---

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

main方法:

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package sxjm;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.SWT;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.graphics.Point;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.layout.GridData;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.layout.GridLayout;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.widgets.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import swtDemo.ImageFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ToolBarSample ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static void main(String args\[])  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Display d = new Display();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Shell s = new Shell(d);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        s.setLayout(new GridLayout());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Composite tool  =  new Composite(s,SWT.NONE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        tool.setLayoutData(new GridData(SWT.LEFT,SWT.TOP,true,false));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ToolBar toolBar = new ToolBar(tool,SWT.NONE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ToolItem saveItem = new ToolItem(toolBar,SWT.PUSH);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        saveItem.setImage(ImageFactory.loadImage(d,ImageFactory.SAVE\_EDIT));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        saveItem.setText("save");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        toolBar.pack();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        s.setSize(new Point(200,200));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        s.open();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        while(!s.isDisposed())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if(!d.readAndDispatch())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                d.sleep();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        ImageFactory.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        d.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

下面是

ImageFactory类

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package swtDemo;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Enumeration;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.util.Hashtable;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.graphics.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.eclipse.swt.widgets.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class ImageFactory ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    private ImageFactory()...{}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //配置图片的绝对路径  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static  final String REAL\_PATH = "E:\\workspace\\icons\\";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //一些图片名称的常量  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static final String DELETE\_EDIT="delete\_edit.jpg";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static final String SAVE\_EDIT="save\_edit.jpg";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static final String COPY\_EDIT="copy\_edit.jpg";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static final String PRINT\_EDIT="print\_edit.jpg";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //..........  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private static  Hashtable htImage = new Hashtable();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static Image loadImage(Display display , String imageName)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Image image = (Image)htImage.get(imageName.toUpperCase());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if(image==null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                image = new Image(display,REAL\_PATH+imageName);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                htImage.put(imageName.toUpperCase(),image);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return image;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public static void dispose()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Enumeration e = htImage.elements();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            while(e.hasMoreElements())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                Image image = (Image)e.nextElement();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if(!image.isDisposed())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    image.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

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

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

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//它将数字的名称用作键：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Hashtable numbers = new Hashtable();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)numbers.put("one", new Integer(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) numbers.put("two", new Integer(2));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)numbers.put("three", new Integer(3));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//要检索一个数字，可以使用以下代码：  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Integer n = (Integer)numbers.get("two");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) if (n != null) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println("two = " + n);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

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

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//实现 Enumeration 接口的对象，它生成一系列元素，一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//例如，要输出向量 v 的所有元素，可使用以下方法：   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         System.out.println(e.nextElement());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//这些方法主要通过向量的元素、哈希表的键以及哈希表中的值进行枚举。枚举也用于将输入流指定到 SequenceInputStream 中。   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

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