• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: RegisterFont
title: GraphicsEnvironmentにFontを登録して使用する
tags: [Font, GraphicsEnvironment, Html, StyleSheet, JLabel, JEditorPane, JTextPane]
author: aterai
pubdate: 2009-10-19T14:42:22+09:00
description: GraphicsEnvironmentにFontを登録して、Htmlタグなどで使用できるようにします。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRkZgST0I/AAAAAAAAAhY/czEyIQL0NfY/s800/RegisterFont.png
---
* 概要 [#summary]
`GraphicsEnvironment`に`Font`を登録して、`Html`タグなどで使用できるようにします。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRkZgST0I/AAAAAAAAAhY/czEyIQL0NfY/s800/RegisterFont.png)

* サンプルコード [#sourcecode]
#code(link){{
Font font = makeFont(getClass().getResource("Burnstown Dam.ttf"));
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
}}

* 解説 [#explanation]
- `1`: `JLabel#setFont`メソッドでフォントを設定して使用
- `1`: `JLabel#setFont(...)`メソッドでフォントを設定して使用
- `2`: 登録したフォントを`Html`タグで指定して使用
-- `label.setText("<html><font size='+3' face='Burnstown Dam'>2: html,font,size,+3</font></html>");`
-- `label.setText("<html><font size='+3' face='Burnstown Dam'>2: html,font,size,+3</font></html>")`
- `3`: `StyleSheet`で`body`タグのフォントを設定して使用
-- `styleSheet.addRule("body {font-size: 24pt; font-family: Burnstown Dam;}");`
- `4`: `JTextPane#setFont`メソッドでフォントを設定して使用
-- `body`タグにデフォルトで指定されているフォントを無視して、`JTextPane`のデフォルトのフォントを使用したい場合は、`editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);`とする必要がある
-- `styleSheet.addRule("body {font-size: 24pt; font-family: Burnstown Dam;}")`
- `4`: `JTextPane#setFont(...)`メソッドでフォントを設定して使用
-- `body`タグにデフォルトで指定されているフォントを無視して`JTextPane`のデフォルトのフォントを使用したい場合は、`editor.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE)`とする必要がある
-- [[JEditorPaneに設定したフォントをHTMLテキストに適用する>Swing/HonorDisplayProperties]]

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/GraphicsEnvironment.html#registerFont-java.awt.Font- GraphicsEnvironment#registerFont(Font) (Java Platform SE 8)]
- [http://www.creamundo.com/ creamundo | Fuentes Gratis TrueType TTF]
- [[Fontをファイルから取得>Swing/CreateFont]]
- [[Htmlを使ったJLabelとJEditorPaneの無効化>Swing/DisabledHtmlLabel]]
- [[JEditorPaneのHTMLEditorKitにCSSを適用>Swing/StyleSheet]]

* コメント [#comment]
#comment
#comment