将JTextPane设置为内容类型HTML并使用字符串构建器

ora*_*nge 5 html java swing jtextpane text-decorations

我正在使用字符串构建器将文本附加到我的JTextPane,我已将内容类型设置为pane.setContentType("text/html");但我的JTextPane上实际上没有显示文本.

这是我追加的一个例子:

buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");
Run Code Online (Sandbox Code Playgroud)

有什么我做错了吗?我该如何解决这个问题呢?

Joo*_*gen 9

每次JTextPane.setText(...)调用时都会确定新的内容类型.用文本开始"<html>",你就有了HTML.

在您的案例中创建一个新文档HTMLDocument.


@mKorbel:以下每次为JTextPane创建HTML.

    buildSomething.append("<html>");
    buildSomething1.append("<html>");
    for (int i = 0; i < 10; i++) {
        buildSomething.append("<span style=\"color:red\">" + myBirthday + "</span>");
        buildSomething1.append("<b style=\"color:blue\">" + myBirthday + "</b>");
    }
Run Code Online (Sandbox Code Playgroud)

  • 我不得不将JTextPane中的属性`contentType`设置为`text/html`以使其工作. (13认同)

mKo*_*bel 8

@Joop Eggen

1.循环生成

buildSomething.append("<span style=\"color:pink\">" + myBirthday + "</span>");
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

第2位.循环生成相同的输出,我认为如果被包裹在里面<html> ..<html>并不重要因为有pane.setContentType("text/html");

和(我在这里发布的代码不正确<html> ..</html>)

buildSomething1.append("<html><span style=\"color:pink\">" 
    + myBirthday + "</span></html>");
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;

public class MyTextPane implements Runnable {

    private JFrame frm;
    private JScrollPane jsp;
    private JTextPane jta;
    private StringBuilder buildSomething = new StringBuilder();
    private StringBuilder buildSomething1 = new StringBuilder();
    final String myBirthday = "Birthday";

    public MyTextPane() {
        for (int i = 0; i < 10; i++) {
            buildSomething.append("<span style=\"color:red\">" + myBirthday + "</span>");
            buildSomething1.append("<span style=\"color:blue\">" + myBirthday + "</span>");
        }
        jta = new JTextPane();
        jta.setContentType("text/html");
        jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        jta.setText(myBirthday);
        jsp = new JScrollPane(jta);
        jsp.setPreferredSize(new Dimension(250, 450));
        frm = new JFrame("awesome");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLayout(new BorderLayout());
        frm.add(jsp, BorderLayout.CENTER);
        frm.setLocation(100, 100);
        frm.pack();
        frm.setVisible(true);
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1500);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                jta.setText(null);
                HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
                try {
                    doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething.toString());
                } catch (BadLocationException ex) {
                    Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        try {
            Thread.sleep(1500);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
                try {
                    doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething1.toString());
                } catch (BadLocationException ex) {
                    Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyTextPane fs = new MyTextPane();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)