2016-06-16 8 views
0

JEditorPaneのCSSスタイルを変更するにはどうすればよいですか?JEditorPaneの以前に設定されたCSSスタイルの変更

StyleSheet styleSheet = new StyleSheet(); 
styleSheet.addRule("body {font-family:"Arial"; font-size:12; } "); 

HTMLEditorKit hTMLEditorKit = new HTMLEditorKit(); 
hTMLEditorKit.setStyleSheet(styleSheet); 

JEditorPane editorPane = new JEditorPane("text/html", ""); 
editorPane.setEditorKit(messageEditorPaneHTMLEditorKit); 

ここで、このスタイルを変更したいと思います(この場合は別のフォントを設定します)。私はこれを試しましたが、何もしません。スタイルは変わりません。

Style style = hTMLEditorKit.getStyleSheet().getRule("body"); 
style.addAttribute("font-family", "Helvetica"); 
style.addAttribute("font-size", 14); 

スタイルを変更するにはどうすればよいですか?

答えて

1

あなたが変更することができStyleなど"font-size"

は、たとえば、あなたが修正することができるあなたが文字列リテラルへの依存を減らす変更できるプロパティを提供するという付加的な利点を持っているStyleConstantsを、使用して:

style.addAttribute("font-family", "Helvetica"); 
style.addAttribute("font-size", 14); 

へ:

StyleConstants.setFontSize(style, 14); 
StyleConstants.setFontFamily(style, "Helvetica"); 

あなたがprintStyleAttributesメソッドを使用している場合は、私がすること含めました低い場合は、変更内容がStyleに反映されます。ただし、エディタペインに変更が自動的に適用されることはありません

スタイリングの変更が反映されるためには、文書に適用する必要がある場所と既存のものを上書きするかどうかを指定するために、setCharacterAttributes()を文書に使用する必要があります。スタイルは

例:見つかりました変更でのスタイルを置き換え、document.setCharacterAttributes(0, document.getLength(), style, true);

は、文書全体を更新するの


SSCCE:

import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.html.HTMLDocument; 
import javax.swing.text.html.HTMLEditorKit; 
import javax.swing.text.html.StyleSheet; 
import java.awt.*; 
import java.util.Enumeration; 

public class EditorStylingExample { 
    public static void main(String[] args) { 
     StyleSheet styleSheet = new StyleSheet(); 
     styleSheet.addRule("body {font-family:\"Arial\"; font-size:12; } "); 

     HTMLEditorKit messageEditorPaneHTMLEditorKit = new HTMLEditorKit(); 
     messageEditorPaneHTMLEditorKit.setStyleSheet(styleSheet); 
     HTMLDocument document = (HTMLDocument) messageEditorPaneHTMLEditorKit.createDefaultDocument(); 

     JEditorPane editorPane = new JEditorPane("text/html", ""); 
     editorPane.setEditorKit(messageEditorPaneHTMLEditorKit); 
     editorPane.setDocument(document); 

     JButton changeStyleButton = new JButton("Change style"); 
     changeStyleButton.addActionListener(e -> { 
      Style style = styleSheet.getStyle("body"); 
      StyleConstants.setBold(style, true); 
      StyleConstants.setFontSize(style, 14); 
      StyleConstants.setFontFamily(style, "Helvetica"); 
      printStyleAttributes(style); 
      document.setCharacterAttributes(0, document.getLength(), style, true); 
     }); 

     JFrame frame = new JFrame("Styling example"); 
     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 

     contentPane.add(editorPane); 
     contentPane.add(changeStyleButton); 

     editorPane.setAlignmentX(Component.CENTER_ALIGNMENT); 
     changeStyleButton.setAlignmentX(Component.CENTER_ALIGNMENT); 

     frame.setContentPane(contentPane); 
     frame.setSize(300,300); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private static void printStyleAttributes(Style style) { 
     Enumeration styleAttributes = style.getAttributeNames(); 
     while (styleAttributes.hasMoreElements()) { 
      Object attribute = styleAttributes.nextElement(); 
      String attributeName = attribute.toString(); 
      Object attributeValue = style.getAttribute(attribute); 
      System.out.println(attributeName + ": " + attributeValue); 
     } 
    } 
} 
+0

うわー!これは私を大きく助けました。ありがとう! StyleConstants.setFontSize(style、14);フォントサイズを14に設定していないのに何らかの理由で5または6に設定されているようです。 – ed22

+1

嬉しいことです:)あなたが提供した値はまだバックグラウンドで残っています。これは 'System.out.println(StyleConstants.getFontSize(style)); 'で確認できます。サポートされているfont-sizeは8で、出力では1です – Peter

関連する問題