在JEditorPane中设置默认字体

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

这不会改变文本的字体。我需要知道如何使用HTML编辑器工具包为JEditorPane设置默认字体。

编辑:

输入图像描述


5
请以文本格式发布您的代码,而不是其图像。因为任何想测试它的人都需要将其手动输入。这不是学校 :) - David Kroukamp
关于如何进行屏幕截图的更多信息,请参见 此处 - trashgod
5个回答

31

试试这个:

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

感谢博主de-co-de的分享!原文链接: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());

完美运行。


23

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

2

由于您正在使用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语句。


1

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

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

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

编辑2: 将您的主方法更改如下。它设置了Nimbus LookAndFeel。我还没有检查其他的LookAndFeel。

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

正如我在编辑后的答案中提到的那样,请确保您已经实现了精确的代码。 - Canis Majoris
请观察上传的图片,因为文本不是Segoe Script字体。 - Sanjeev
也许你的平台不支持它。你的操作系统是什么?再看一下我的帖子,我告诉你如何设置Nimbus LookAndFeel。试试看,肯定会起作用的。 - Canis Majoris

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

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


1
不错,但这是JTextPane的一个示例。 - David Kroukamp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接