Loading custom fonts at runtime for use with JTextPane

感情迁移 提交于 2020-01-04 06:14:49

问题


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.

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!


回答1:


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.

A JTextPane can use multiple fonts.

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.

Edit:

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

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

GraphicsEnvironment.registerFont(Font)


来源:https://stackoverflow.com/questions/7800683/loading-custom-fonts-at-runtime-for-use-with-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!