将字体添加到 Swing 应用程序并包含在包中 [英] Adding fonts to Swing application and include in package

查看:25
本文介绍了将字体添加到 Swing 应用程序并包含在包中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 Java Swing 应用程序中使用自定义字体 (ttf).如何将它们添加到我的包中并使用它们?

I need to use custom fonts (ttf) in my Java Swing application. How do I add them to my package and use them?

同时,我只是在windows中安装它们然后我使用它们,但我不希望应用程序的使用如此复杂,在使用之前告诉用户安装字体不是很方便我的申请.

Mean while, I just install them in windows and then I use them, but I don't wish that the usage of the application will be so complicated, it`s not very convenient to tell the user to install fonts before using my application.

推荐答案

您可以通过 InputStream 加载它们:

You could load them via an InputStream:

InputStream is = MyClass.class.getResourceAsStream("TestFont.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);

这个加载的字体没有预定义的字体设置,所以要使用,你必须这样做:

This loaded font has no predefined font settings so to use, you would have to do:

Font sizedFont = font.deriveFont(12f);
myLabel.setFont(sizedFont);

见:

物理和逻辑字体

这篇关于将字体添加到 Swing 应用程序并包含在包中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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