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

时间:2012-01-30 21:56:29

标签: java html swing jtextpane text-decorations

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

这是我追加的一个例子:

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

我有什么问题严重错误吗?我该如何解决这个问题呢?

2 个答案:

答案 0 :(得分: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>");
    }

答案 1 :(得分:8)

@Joop Eggen

第一。循环生成

buildSomething.append("<span style=\"color:pink\">" + myBirthday + "</span>");

enter image description here

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

和(我在此处<html> ..</html>张贴的代码不正确)

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

enter image description here

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