2012-09-22 5 views
9
editorPane.setContentType("text/html");  
editorPane.setFont(new Font("Segoe UI", 0, 14)); 
editorPane.setText("Hello World"); 

これはテキストのフォントを変更しません。 HTMLエディタキットでJEditorPaneのデフォルトフォントを設定する方法を知る必要があります。JEditorPaneのデフォルトフォントを設定する

編集:私はあなたのコードをチェックしました

enter image description here

+4

コードをテキスト形式で投稿してください。テストする人は誰でも書き出せなければならないので、コードはイメージではありません。これは学校ではありません:) –

+0

[screenshot]を取る方法の詳細(http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-役職)。 – trashgod

答えて

1

、どんな問題があってはなりません。他のフォントをテストしましたか? "Segoe Script"フォントを試してみてください。

編集: 私はこのコードを試してみましたが、うまく動作します。投稿したコードは、実装したコードとまったく同じですか?

editorPane.setContentType("text/html"); 
    editorPane.setFont(new Font("Segoe Script", 0, 14)); 
    editorPane.setText("it works!"); 

EDIT2:は次のようにあなたのmainメソッドを変更し 。 Nimbus LookAndFeelを設定します。私はまだ他のLookAndFeelsをチェックしていない。

public static void main(String[] args) 
{ 
    try 
    { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
     { 
      if ("Nimbus".equals(info.getName())) 
      { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) 
    { 
     java.util.logging.Logger.getLogger(EditorPaneDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 

    java.awt.EventQueue.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      new EditorPaneDemo(); 
     } 
    }); 
} 
+1

コードは機能しますが、フォントは更新されません。 – Sanjeev

+0

私が編集した回答に記載した通り、正確なコードを実装していることを確認してください –

+0

テキストはフォントsegoeスクリプトではないので、アップロードした画像を確認してください。 – Sanjeev

1

試し以下

editorPane.setFont(new Font("Segoe UI", Font.PLAIN, 24)); 

下のコードを働いている:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Document; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.StyleConstants; 

public class jeditorfont extends JFrame { 
    private JTextPane textPane = new JTextPane(); 

    public jeditorfont() { 
    super(); 
    setSize(300, 200); 

    textPane.setFont(new Font("Segoe UI", Font.PLAIN, 24)); 

    // create some handy attribute sets 
    SimpleAttributeSet red = new SimpleAttributeSet(); 
    StyleConstants.setForeground(red, Color.red); 
    StyleConstants.setBold(red, true); 
    SimpleAttributeSet blue = new SimpleAttributeSet(); 
    StyleConstants.setForeground(blue, Color.blue); 
    SimpleAttributeSet italic = new SimpleAttributeSet(); 
    StyleConstants.setItalic(italic, true); 
    StyleConstants.setForeground(italic, Color.orange); 

    // add the text 
    append("NULL ", null); 
    append("Blue", blue); 
    append("italic", italic); 
    append("red", red); 

    Container content = getContentPane(); 
    content.add(new JScrollPane(textPane), BorderLayout.CENTER); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    protected void append(String s, AttributeSet attributes) { 
    Document d = textPane.getDocument(); 
    try { 
     d.insertString(d.getLength(), s, attributes); 
    } catch (BadLocationException ble) { 
    } 
    } 

    public static void main(String[] args) { 
    new jeditorfont().setVisible(true); 
    } 
} 

REF:http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetFontFontfont.htm

+1

これは 'JTextPane'の例です –

16

HTMLをレンダリングし、JEditorPaneののフォントは、そのスタイルシートを介して更新する必要があります:

JEditorPane editorPane = 
      new JEditorPane(new HTMLEditorKit().getContentType(),text); 
    editorPane.setText(text); 

    Font font = new Font("Segoe UI", Font.PLAIN, 24)); 
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + 
      "font-size: " + font.getSize() + "pt; }"; 
    ((HTMLDocument)editorPane.getDocument()).getStyleSheet().addRule(bodyRule); 
19

このお試しください:デ - コ - ドブロガーに

JEditorPane pane = new JEditorPane(); 
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); 
pane.setFont(SOME_FONT); 

すべてのクレジットを!出典: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

私はちょうどそれをテストしました。これによりJEditorPaneはJLabelと同じフォントを使用します

JEditorPane pane = new JEditorPane(); 
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); 
pane.setFont(someOrdinaryLabel.getFont()); 

完全に動作します。

1

HTMLツールキットを使用しているときに、標準スタイリングを使用してHTMLでフォントを設定できます。 setTextを次のように変更します。

editorPane.setText("<html><head><style>" + 
        "p {font-family: Segoe UI; font-size:14;}" + 
        "</style></head>" + 
        "<body><p>It Works!</p></body></html>"); 

setFontステートメントを削除します。

関連する問題