簡體   English   中英

HTML Wrapped String 在 JtextPane output 中創建了一個不需要的新行

[英]HTML wrapped String creates a unwanted new line in JtextPane output

當我將帶有 HTML 格式的字符串的字符串和 output 的字符串集中到帶有 HTMLEditor 工具包的 JTextPane 時,包裝在 HTML 標記中的每個附加字符串都會導致出現新行:

// 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 中的 Output 有不需要的換行符,其中每個 HTML 字符串已連接:

在此處輸入圖像描述

預期 output 將是一行中的所有單詞:

你好 gello 頂頂 你好

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

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

我試過修剪字符串,但 output 相同:

saveCurrentSentenceState.trim();

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.

無論如何我可以停止這個換行表單打印嗎?

問題是您插入了不正確的 HTML (正如@AndrewThompson 的評論中提到的那樣)。 在您的情況下,獲得正確結果的最簡單方法是從字符串中刪除所有<html></html>標簽,然后合並這些字符串和 append <html></html>之后。 在這種情況下,您將獲得正確的 HTML,可以由JTextPaneHTMLEditorKit處理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM