Java 在JTextArea或JTextPane中设置文本样式

Java 在JTextArea或JTextPane中设置文本样式,java,html,swing,jtextpane,Java,Html,Swing,Jtextpane,基本上,我有一个JTextPane来保存一些我想要设置样式的文本。JTextArea对我来说会更好用,但我听说你不能用这些来设置文本的样式 但是,JTextPane的样式似乎不正确。例如,返回的代码中包含HTML: public static void main(String[] args) { JFrame j = new JFrame("Hello!"); j.setSize(200,200); JTextPane k = new JTextPane(); k

基本上,我有一个JTextPane来保存一些我想要设置样式的文本。JTextArea对我来说会更好用,但我听说你不能用这些来设置文本的样式

但是,JTextPane的样式似乎不正确。例如,返回的代码中包含HTML:

public static void main(String[] args) {
    JFrame j = new JFrame("Hello!");
    j.setSize(200,200);
    JTextPane k = new JTextPane();
    k.setText("<html><strong>Hey!</strong></html>");
    j.add(k);
    j.setVisible(true);
}
publicstaticvoidmain(字符串[]args){
JFrame j=新的JFrame(“你好!”);
j、 设置大小(200200);
JTextPane k=新的JTextPane();
k、 setText(“嘿!”;
j、 添加(k);
j、 setVisible(真);
}

当用户与界面交互时,我希望能够在JTextPane中设置一些文本的样式,但到目前为止,它只返回HTML仍在的字符串!救命啊

如果要在中显示
Html
内容,则必须设置例如

编辑:

例如,对于
JEditorPanes/JTextPanes
是否有另一种通过实现和定制r的方法


a、 方法是不使用
Html语法

让Java知道它将使用Html方法

我使用的是外观(Substance),调用
setContentType(“text/html”)
显示的字体有问题。我打电话解决了这个问题:

textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
另一个选项是在
JLabel
中包装HTML文本。下面是您的示例代码:

JTextPane k = new JTextPane();
k.insertComponent(new JLabel("<html><strong>Hey!</strong></html>"));
JTextPane k=newjtextpane();
k、 插入组件(新JLabel(“嘿!”);

使用
JTextArea
通常比使用
JLabel
要好,因为
JTextArea
JScrollPane
中滚动更好,并允许用户选择和复制文本。感谢您提供有关实体外观的信息。我也使用它,所以提示是有用的。
JTextPane k = new JTextPane();
k.insertComponent(new JLabel("<html><strong>Hey!</strong></html>"));