在运行时加载自定义字体以与JTextPane一起使用 [英] Loading custom fonts at runtime for use with JTextPane

查看:106
本文介绍了在运行时加载自定义字体以与JTextPane一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的宝贵时间.我的问题是关于在一个JTextPane中显示不同字体的问题.我的客户希望在一个字段中以两种不同的语言查看单词.他们已明确指定希望使用不同的字体显示不同的语言(即Amharic,阿拉伯语,Coptic和希伯来语).这些显然是非标准字体,我不能依靠用户在其操作系统上安装了必需的字体.

Thanks for your time. My question is regarding the display of different fonts within the one JTextPane. My client wishes to view a word in two different languages within the one field. They've explicitly specified that they wish the different languages (namely Amharic, Arabic, Coptic and Hebrew) to be shown with different fonts. These are obviously non-standard fonts and I can't rely on the user having the required fonts installed on their OS.

从我的研究中发现,我可以在运行时加载字体文件并相应地设置JTextPane的字体,如果我只想使用一种字体而不是两种字体,那就很好了.我还阅读了有关将字体添加到OS的字体目录或JRE的字体目录的信息,概述为此处.

From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two. I've also read about adding fonts to the OS' font directory or the JRE's font directory, outlined here.

但是,我希望可以在不更改用户操作系统的情况下使用字体.我不走运吗?

I was hoping however, that there might be away to use the fonts without altering the user's OS. Am I out of luck?

再次感谢您抽出宝贵的时间,我期待有任何出色的想法得到答复!

Thanks again for your time and I look forward to any replies with bright ideas!

推荐答案

从我的研究中发现,我可以在运行时加载字体文件并相应地设置JTextPane的字体,如果我只想使用一种而不是两种字体,那就很好了.

From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two.

JTextPane可以使用多种字体.

A JTextPane can use multiple fonts.

文本组件功能中,从Swing教程中查看该部分. a>作为在文本窗格中使用文本属性的示例.

Check out the section from the Swing tutorial on Text Component Features for an example of playing with the attributes of the text in the text pane.

但是要使用多种字体,我确定设置字体的唯一方法是创建MutableAttributeSet并将"FontFamily"属性(字符串)设置为所需的字体名称,然后将Attribute设置分配给使用StyledDocument.setCharacterAttributes

However to use multiple fonts, the only way I have worked out to set the font is by creating a MutableAttributeSet and setting the "FontFamily" attribute (a string) to the desired font name, and then assigning the Attribute set to the text using the StyledDocument.setCharacterAttributes

阅读createFont()方法的API看起来您应该可以使用:

Reading the API for the createFont() method it looks like you should be able to use:

GraphicsEnvironment.registerFont(Font)

这篇关于在运行时加载自定义字体以与JTextPane一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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