SWT (JFace) 中的 FontRegistry 和 Dispose

FontRegistry and Dispose in SWT (JFace)

我正在学习如何使用 FontRegistry 及其工作原理。 目前我有一个集中式 class (FontUtils),它在启动时加载所有字体数据,如下所示:

fontRegistry.put("small",new FontData[]{new FontData("Tahoma",8, SWT.BOLD)});

稍后在我的程序中我使用这个:

gc.setFont(FontUtils.getFont("small"));

我想知道是否必须处理在 FontUtils 中创建的字体,因为根据文档,每次创建字体时都应该处理它。 但是 FontRegistry 对字体的创建一无所知,实际上它没有名为 dispose 的方法。 我必须自己处理处置吗?

例如使用一个变量来计算有多少资源正在使用该字体,如果为 0 则处置该字体?

因为我要为 ImageRegistry 做同样的事情,所以行为是一样的,对吧?

FontRegistry 管理它拥有的所有字体并处理它们的处置。

JavaDoc 说:

A font registry owns all of the font objects registered with it, and automatically disposes of them when the SWT Display that creates the fonts is disposed. Because of this, clients do not need to (indeed, must not attempt to) dispose of font objects themselves.

这是使用 DisplaydisposeExec 方法完成的。

ImageRegistry 同理:

An image registry owns all of the image objects registered with it, and automatically disposes of them when the SWT Display that creates the images is disposed. Because of this, clients do not need to (indeed, must not attempt to) dispose of these images themselves.

ImageRegistry 也有一个 dispose 方法,如果你想更早地处理图像。

注意:可以使用您自己的 ResourceManager 创建一个 ImageRegistry,在这种情况下,由资源管理器安排处置。