Java 在字体窗格中设置默认字体

Java 在字体窗格中设置默认字体,java,swing,fonts,jeditorpane,Java,Swing,Fonts,Jeditorpane,这不会更改文本的字体。我需要知道如何使用HTML编辑器套件设置JEditorPane的默认字体 编辑: 我已经检查了你的代码,应该没有问题。你测试过其他字体吗?请尝试“Segoe脚本”字体,看看是否有变化 编辑: 我已经试过下面的代码,它对我来说很好。您确定发布的代码与实现的代码完全相同吗 editorPane.setContentType("text/html"); editorPane.setFont(new Font("Segoe UI", 0, 14)); editorPane.

这不会更改文本的字体。我需要知道如何使用HTML编辑器套件设置JEditorPane的默认字体

编辑:


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

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

editorPane.setContentType("text/html");    
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");
Edit2: 改变你的主要方法如下。它设置了Nimbus的外观和感觉。我还没有检查其他外观和感觉

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

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();
        }
    });
}
以下是工作代码:

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

ref:

呈现HTML时,JEditorPane的字体需要通过其样式表进行更新:

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);
  }
}
试试这个:

    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);
所有归功于de-co-deblogger!资料来源:

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

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

工作正常。

使用HTML工具包时,可以使用标准样式在HTML中设置字体。因此,将setText更改为如下内容:

JEditorPane pane = new JEditorPane();
pane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
pane.setFont(someOrdinaryLabel.getFont());
editorPane.setText(“+
“p{字体系列:Segoe UI;字体大小:14;}”+
"" +
“有效!

”;

并删除setFont语句。

正如我在编辑后的回答中提到的,请确保您实现了准确的代码。请观察上传的图像,因为文本不是font segoe脚本。您的平台可能不支持它。你的操作系统是什么?再次查看我的帖子,我向您展示了如何设置Nimbus LookAndFeel。试试看,它一定有用。很好,但这是
JTextPane
的一个示例,请以文本格式发布代码,而不是它的图像,因为任何想要测试它的人都必须将其写出。这不是学校:)更多关于如何参加考试。
editorPane.setText("<html><head><style>" + 
                   "p {font-family: Segoe UI; font-size:14;}" + 
                   "</style></head>" +
                   "<body><p>It Works!</p></body></html>");