java 在 JEditorPane 中设置默认字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12542733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:18:33  来源:igfitidea点击:

Setting default font in JEditorPane

javaswingfontsjeditorpane

提问by Sanjeev

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

This does not change the font of the text. I need to know how to set the default font for the JEditorPane with HTML Editor Kit.

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

Edit:

编辑:

enter image description here

在此处输入图片说明

回答by Espinosa

Try this one:

试试这个:

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

All credits to de-co-deblogger! Source: http://de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

所有学分都归功于de-co-de博主!来源:http: //de-co-de.blogspot.co.uk/2008/02/setting-font-in-jeditorpane.html

I have just tested it. This made JEditorPane to use same font as JLabel

我刚刚测试过。这使得 JEditorPane 使用与 JLabel 相同的字体

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

Works perfectly.

完美运行。

回答by bobby_light

When rendering HTML, JEditorPane's font needs to be updated via its style sheet:

渲染 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);

回答by IanB

As you are using the HTML toolkit you can set the font in the HTML using standard styling. So change the setText to something like this:

当您使用 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>");

and remove the setFont statement.

并删除 setFont 语句。

回答by Canis Majoris

I've checked your code, there shouldn't be any problem. Have you tested other fonts? Please try "Segoe Script" font and see if it changes.

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

Edit:I have tried the code bellow, it works fine for me. Are you sure the code you've posted is exactly the same as you've implemented?

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

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

Edit2:Change your main method as follow. It sets the Nimbus LookAndFeel. I haven't checked other LookAndFeels yet.

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();
        }
    });
}

回答by Alvin Pradeep

try below

试试下面

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

below is working code:

下面是工作代码:

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

参考:http: //www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetFontFontFont.htm