HTML包装的String在JtextPane输出中创建了不需要的新行 [英] HTML wrapped String creates a unwanted new line in JtextPane output

查看:111
本文介绍了HTML包装的String在JtextPane输出中创建了不需要的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将字符串与HTML格式的字符串集中时,将字符串输出到带有 HTMLEditor套件,HTML标记中包装的每个附加的String似乎都会引起新的一行:

When I concentrate a String with a HTML formatted string, and output the String to a JTextPane with a HTMLEditor kit, every appended String wrapped in the HTML tags appears to cause a new line:

// Set the HTML Editor kit for JTExtPAne
    jtextPane.setEditorKit(new HTMLEditorKit());

        String  saveCurrentSentenceState = "Some String";
        String newWord = "new word"; // wrap this in HTML tags

        // Create a HTML String
        String appendHTML = "<html><font color=\"red\">"+newWord+"<</font>";

        // Concatenate with an existing String
        saveCurrentSentenceState += " " + appendHTML;

        jtextPane.setText(appendHTML);

JTextPane中的输出有多余的换行符,其中每个HTML字符串都已串联:

Output in the JTextPane has unwanted line breaks where each HTML String has been concatenated:

预期的输出将是一行中的所有单词:

Expected output would be all the words in a single line:

你好,你好,顶部,你好

hello gello top top hello

这是打印到控制台的字符串:

This is the string printed to the console:

 hello gello <html><font color="red">top<</font> <html><font color="red">top<</font> hello

我尝试修剪字符串但输出相同:

I have tried trimming the string but same output:

saveCurrentSentenceState.trim();

当我在字符串后附加HTML格式的子字符串时,我不会关闭HTML标签,因为在关闭的HTML标签之后的任何串联字符串都不会打印.

As I append the String with a HTML formatted sub string, I do not close the HTML tag, as any concatenated string after a closed HTML tag does not print.

反正我可以停止打印换行符吗?

Is there anyway I can stop this newline form printing ?

推荐答案

问题是您插入了错误的HTML(如@AndrewThompson的注释中所述).要获得正确结果,最简单的方法是从字符串中删除所有<html></html>标记,然后合并这些字符串并在其后附加<html></html>.在这种情况下,您将获得正确的HTML,可以由JTextPaneHTMLEditorKit

The problem is that you inserts the incorrect HTML (as it was mentioned in the comment of @AndrewThompson). The easiest way in your case to get the correct result is to remove all <html> and </html> tags from your strings, then merge these strings and append <html> and </html> after that. In this case you'll get the correct HTML, that can be processed by the HTMLEditorKit of JTextPane

这篇关于HTML包装的String在JtextPane输出中创建了不需要的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆