Swing/LineSpacing のバックアップソース(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/LineSpacing へ行く。
- 1 (2009-11-02 (月) 12:51:40)
- 2 (2009-11-03 (火) 06:00:48)
- 3 (2010-03-08 (月) 12:30:38)
- 4 (2010-03-08 (月) 13:46:04)
- 5 (2011-04-12 (火) 20:58:07)
- 6 (2013-01-04 (金) 16:10:51)
- 7 (2013-08-13 (火) 19:38:01)
- 8 (2015-03-05 (木) 00:34:06)
- 9 (2015-03-05 (木) 14:55:01)
- 10 (2015-03-09 (月) 14:46:02)
- 11 (2015-03-16 (月) 17:28:33)
- 12 (2016-05-26 (木) 15:12:24)
- 13 (2017-08-16 (水) 17:28:32)
- 14 (2018-08-17 (金) 13:34:59)
- 15 (2018-12-13 (木) 18:33:12)
- 16 (2020-11-11 (水) 02:09:47)
- 17 (2022-11-11 (金) 14:47:02)
- 18 (2024-02-14 (水) 23:39:31)
TITLE:JEditorPaneやJTextPaneに行間を設定する #navi(../) #tags(JEditorPane, JTextPane, StyledEditorKit) RIGHT:Posted by &author(aterai); at 2009-11-02 *JEditorPaneやJTextPaneに行間を設定する [#u308ecd7] ``JEditorPane``や``JTextPane``に行間を設定します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTPYZn_u9I/AAAAAAAAAd4/5-1ThpWwM5U/s800/LineSpacing.png) **サンプルコード [#x743f159] #code(link){{ class BottomInsetEditorKit extends StyledEditorKit { @Override public ViewFactory getViewFactory() { return new ViewFactory() { @Override public View create(Element elem) { String kind = elem.getName(); if (kind!=null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new LabelView(elem); } else if (kind.equals(AbstractDocument.ParagraphElementName)) { return new javax.swing.text.ParagraphView(elem) { @Override protected short getBottomInset() { return 5; } }; } else if (kind.equals(AbstractDocument.SectionElementName)) { return new BoxView(elem, View.Y_AXIS); } else if (kind.equals(StyleConstants.ComponentElementName)) { return new ComponentView(elem); } else if (kind.equals(StyleConstants.IconElementName)) { return new IconView(elem); } } return new LabelView(elem); } }; } } }} **解説 [#oa458f2f] -上: ``StyleConstants.setLineSpacing``で、行間を指定した``AttributeSet``を作成し、``JTextPane#setParagraphAttributes``で設定しています。 -- フォントサイズ相対の行間になる #code{{ SimpleAttributeSet a = new SimpleAttributeSet(); StyleConstants.setLineSpacing(a, .5f); //StyleConstants.setSpaceAbove(a, 5.0f); //StyleConstants.setSpaceBelow(a, 5.0f); //StyleConstants.setLeftIndent(a, 5.0f); //StyleConstants.setRightIndent(a, 5.0f); editor1.setParagraphAttributes(a, true); setDummyText(editor1); }} -下: ``ParagraphView#getBottomInset``をオーバーライドして、固定の行間をピクセルで指定しています。 -- フォントサイズに関係なく、アキ``5px`` ---- - スタイルシートで``line-height``を指定しても反映されない(``line-height``は、モデル化されているが、現在は描画されない) -- [http://docs.oracle.com/javase/jp/6/api/javax/swing/text/html/CSS.html 対応しているCSSプロパティ一覧 - CSS (Java Platform SE 6)] #code{{ StyleSheet styleSheet = new StyleSheet(); styleSheet.addRule("body {font-size: 24pt; line-height: 2.0}"); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); htmlEditorKit.setStyleSheet(styleSheet); editor1.setEditorKit(htmlEditorKit); editor1.setText("<html><body>12<br />a<br />n<font size='32'>123<br />sd</font></body></html>"); }} //**参考リンク **コメント [#q7d6c5ab] #comment