在JEditorPane中设置默认字体

San*_*eev 12 java fonts swing jeditorpane

editorPane.setContentType("text/html");    
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");
Run Code Online (Sandbox Code Playgroud)

这不会改变文本的字体.我需要知道如何使用HTML Editor Kit设置JEditorPane的默认字体.

编辑:

在此输入图像描述

Esp*_*osa 22

试试这个:

JEditorPane pane = new JEditorPane();
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
pane.setFont(SOME_FONT);
Run Code Online (Sandbox Code Playgroud)

de-co-de blogger的所有学分!资料来源: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());
Run Code Online (Sandbox Code Playgroud)

完美的工作.


bob*_*ght 19

渲染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);
Run Code Online (Sandbox Code Playgroud)


Can*_*ris 1

我检查了你的代码,应该没有问题。你测试过其他字体吗?请尝试“Segoe Script”字体,看看是否有变化。

编辑: 我已经尝试过下面的代码,它对我来说效果很好。您确定您发布的代码与您实现的代码完全相同吗?

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

Edit2: 按如下方式更改您的主要方法。它设置 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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)