Swing 加载可用的字体系列会降低性能 [英] Swing load available font family slow down the performance

查看:41
本文介绍了Swing 加载可用的字体系列会降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我添加此代码以加载可用的字体系列并将其添加到组合框后

After I add in this code to load the available font family and add it in to combobox

 GraphicsEnvironment ge = GraphicsEnvironment.
  getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();

当我触发页面时,加载速度非常慢,大约需要 7 秒才能显示页面.我把它拿出来后,它正常加载.有没有解决方案,有遇到同样问题的人吗?

It load very slow took about 7 seconds to show the page when i trigger the page. After i take it out it load fine as normal. Is there any solution, any people faceing the same problem?

推荐答案

延迟是由于 getAvailableFontFamilyNames 为它可以找到的每种字体创建一个 1 pt 字体.它允许 JVM 区分它可以使用的字体和那些看起来可能是字体的东西.

The delay is due to the fact that getAvailableFontFamilyNames creates a 1 pt font for every font it can find. It allows the JVM to distinguish between fonts it can use and things that only look like they may be fonts.

最好的方法是在 SwingWorker 中调用它,然后从 done 方法更新组合.

The best approach is to call it in a SwingWorker and then update the combo from the done method.

更新:海报的代码更新为使用泛型的 SwingWorker.注意:我正在返回名称数组,因为它消除了同步的需要.

update: The poster's code updated to use the generified SwingWorker. Note: I am returning the array of names as it eliminates the need for synchronization.

SwingWorker aWorker<String[],Void> = new SwingWorker<String[],Void>() {
    protected void done() { 
        String[] fontNames = get();
        for (int i = 0; i < fontNames.length; i++) 
            fontFamily.addItem(fontNames[i]);
    }
    @Override
    protected String[] doInBackground() throws Exception {
        GraphicsEnvironment env = GraphicsEnvironment .getLocalGraphicsEnvironment();
        return env.getAvailableFontFamilyNames(); 
    }
};
aWorker.run();

这篇关于Swing 加载可用的字体系列会降低性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆