简体   繁体   English

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

[英]setting JTextPane to content type HTML and using string builders

I'm using string builders to append text to my JTextPane, I've set content type as pane.setContentType("text/html"); 我正在使用字符串构建器将文本追加到我的JTextPane,我将内容类型设置为pane.setContentType("text/html"); but the no text actually appears on my JTextPane. 但是没有文字实际出现在我的JTextPane上。

This is an example of my append: 这是我追加的一个例子:

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

Is there something I'm doing severely wrong? 有什么我做错了吗? And how do I go about fixit it? 我该如何解决这个问题呢?

Every time JTextPane.setText(...) is called a new content type is determined. 每次调用JTextPane.setText(...)都会确定新的内容类型。 Start the text with "<html>" and you've got HTML. "<html>"开始文本,你就得到了HTML。

A new document is created, in your case HTMLDocument. 在您的案例中创建一个新文档HTMLDocument。


@mKorbel: the following creates every time HTML for the JTextPane. @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>");
    }

@Joop Eggen @Joop Eggen

1st. 1。 loop generate 循环生成

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

在此输入图像描述

2nd. 第2位。 loop generate same output, I think that doesn't matter if is wrapped inside <html> ..<html> or not because there is pane.setContentType("text/html"); 循环生成相同的输出,我认为无论是否包含在<html> ..<html>都没关系,因为有pane.setContentType("text/html");

and (not correct code that I posted here <html> ..</html> ) 和(不是我在这里发布的正确代码<html> ..</html>

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

在此输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM