editorPane.setContentType("text/html");
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");
这不会改变文本的字体。我需要知道如何使用HTML编辑器工具包设置JEditorPane的默认字体。在JEditorPane中设置默认字体
编辑:
editorPane.setContentType("text/html");
editorPane.setFont(new Font("Segoe UI", 0, 14));
editorPane.setText("Hello World");
这不会改变文本的字体。我需要知道如何使用HTML编辑器工具包设置JEditorPane的默认字体。在JEditorPane中设置默认字体
编辑:
我检查你的代码,不应该有任何问题。你测试过其他字体吗?请尝试“Segoe Script”字体,看看它是否发生变化。
编辑: 我试过了代码波纹管,它适用于我。您确定您发布的代码与您实施的代码完全相同吗?
editorPane.setContentType("text/html");
editorPane.setFont(new Font("Segoe Script", 0, 14));
editorPane.setText("it works!");
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();
}
});
}
试试下面
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
不错,但这是'JTextPane'的一个例子 –
当渲染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);
试试这个:
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());
完美地工作。
当您使用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语句。
请将您的代码以文本格式发布,而不是它的图像,因为任何想要测试它的人都必须将其写出来。这不是学校:) –
更多关于如何采取[截图](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-后)。代码可以使用 – trashgod