背景:
SWT的图像创建是重要级的,需要释放资源
JFace封装了ImageRegistry以提供资源自动动释放功能,一般这样做:
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/ImageRegistryDemo.htm
import org.eclipse.jface.resource.*;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class tests ImageRegistry
*/
public class ImageRegistryTest extends ApplicationWindow {
// Keys for the registry
private static final String ONE = "one";
private static final String TWO = "two";
private static final String THREE = "three";
/**
* ImageRegistryTest constructor
*/
public ImageRegistryTest() {
super(null);
}
/**
* Runs the application
*/
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
/**
* Creates the window's contents
*
* @param parent the parent composite
* @return Control
*/
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new FillLayout());
// Put the images in the registry
ImageRegistry ir = new ImageRegistry();
ir.put(ONE, ImageDescriptor.createFromFile(ImageRegistryTest.class,
"java2s.gif"));
ir.put(TWO, ImageDescriptor.createFromFile(ImageRegistryTest.class,
"java2s.gif"));
ir.put(THREE, ImageDescriptor.createFromFile(ImageRegistryTest.class,
"java2s.gif"));
// Create the labels and add the images
Label label = new Label(composite, SWT.NONE);
label.setImage(ir.get(ONE));
label = new Label(composite, SWT.NONE);
label.setImage(ir.get(TWO));
label = new Label(composite, SWT.NONE);
label.setImage(ir.get(THREE));
getShell().pack();
return composite;
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new ImageRegistryTest().run();
}
}
但是一般应用程序图片比较多,图片增加,删除,修改名称时,都要动这个文件,注册起来比较麻烦,于是想了一个办法,把图片放到一个目录里,然后,遍历目录自动注册,以图片名称作为key
public class Registry {
private static Logger logger = LoggerFactory.getLogger(Registry.class);
private static String imagePath = Registry.class.getResource("images/").getPath();
private static ImageRegistry imageRegistry = JFaceResources.getImageRegistry();
static {
initRegistry();
}
public static Image getImage(String key) {
Image image = imageRegistry.get(key);
if (image == null) {
image = ImageDescriptor.getMissingImageDescriptor().createImage();
}
return image;
}
public static ImageDescriptor getDescriptor(String key) {
ImageDescriptor descriptor = imageRegistry.getDescriptor(key);
if (descriptor == null) {
descriptor = ImageDescriptor.getMissingImageDescriptor();
}
return descriptor;
}
private static void initRegistry() {
Collection<File> files = FileUtils.listFiles(new File(imagePath), null, false);
for (File file : files) {
imageRegistry.put(file.getName(),
ImageDescriptor.createFromFile(Registry.class, "images/" + file.getName()));
}
}
}
注意一点,此类在做单元测试时要注意:一定要搞个shell出来,不然JFaceResources.getImageRegistry()会报空!调了N长时间
单元测试参考代码:
@Test
public void testImageRegistry() {
Display display = new Display();
Shell shell = new Shell(display, SWT.NO);
shell.setImage(display.getSystemImage(SWT.ICON_WARNING));
shell.setBounds(100, 100, 640, 480);
shell.setText("testImageRegistry");
assertNotNull(JFaceResources.getImageRegistry());// must initialize a shell!!!
// shell.open();
// while (!shell.isDisposed()) {
// if (!display.readAndDispatch())
// display.sleep();
// }
// display.dispose();
}
原文链接: http://blog.csdn.net/kimsoft/article/details/7273196