java - SWT (JFace) 中的 FontRegistry 和 Dispose

标签 java swt jface

我正在学习如何使用 FontRegistry 及其工作原理。 目前我有一个集中类(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,在这种情况下,由资源管理器来安排处置。

关于java - SWT (JFace) 中的 FontRegistry 和 Dispose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46388925/

相关文章:

java - JButton 的不透明度/半透明度?

java - 处理大文件的正确方法是什么?

java - SWT 中的米勒柱?

java - 在 MacOSX 上部署 Java 应用程序(从 Windows 系统)

java - ScrolledPageBook 的替代品?

java - JFace 操作的定义 ID 是什么?

java - 无法刷新 jface 表查看器

java - ResultSet.next() 抛出 SQLException : Result Set Closed

java - 为什么 javac 提示没有初始化变量?

java - SWT Splitpane 可滚动