简体   繁体   English

从 HTML 文件读取文本时,JTextPane 未添加新行

[英]JTextPane is not adding a new line when reading text from HTML file

I am attempting to read text from an HTML file based on the id of the element and display it in a JTextPane.我正在尝试根据元素的 id 从 HTML 文件中读取文本并将其显示在 JTextPane 中。 However, when I read in the text and display it, it is not going to a new line.但是,当我阅读文本并显示它时,它不会换行。 Here is an example of the HTML being read in:这是正在读取的 HTML 的示例:

<!-- LOOK BED command text -->
<p id="look-bed">
    The bed looks hard and uncomfortable.<br>
</p>


<!-- LOOK FOOD command text -->
<p id="look-food">
    The food doesn't look very appetizing.<br>
</p>

The method that I am using to read from the HTML page is:我用来读取 HTML 页面的方法是:

   public static String ReadFromHTMLFile(String tagId) {
        Document htmlFile;
        File file = new File("GameText.html");
        try {
            htmlFile = Jsoup.parse(file, "UTF-8");
            Element element = htmlFile.getElementById(tagId);
            return String.valueOf(element);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return "Unable to access " + tagId + " element";
    }

The method that I am using to update the text in the JTextPane is:我用来更新 JTextPane 中的文本的方法是:

   public static void SetGameText(String location, String text, boolean appendText) {
        if(appendText) {
            try {
                HTMLDocument doc = (HTMLDocument) gameText.getStyledDocument();
                doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), text);
            }
            catch (IOException | BadLocationException e) {
                e.printStackTrace();
            }
        }
        else {
            gameText.setText("");
            gameText.setText(location + text);
        }
    }

The user is supposed to enter a command in a text box and click the submit button and get feedback based on the command that was entered.用户应该在文本框中输入命令并单击提交按钮并根据输入的命令获得反馈。 The problem is that each of the commands that are entered appear on the same line instead of a new line.问题是输入的每个命令都出现在同一行而不是换行。

The part of the SetGameText method that runs if appendText is false functions the way it is supposed to, creating line breaks where I want them, but the part that runs if appendText is true is not creating a line break, even though I am using the <br> tag in the HTML.如果 appendText 为 false,则运行的 SetGameText 方法部分按预期方式运行,在我想要的位置创建换行符,但如果 appendText 为 true,则运行的部分不会创建换行符,即使我使用的是<br> 标记在 HTML 中。

For a reasonable display of HTML documents use JEditorPane .为了合理显示 HTML 文档,请使用JEditorPane Switch it to text/html content and you should be able to get your stuff rendered.将其切换为text/html内容,您应该能够呈现您的内容。

Also check how to make div to new line how to get DIVs on one line each.还要检查如何使 div 到新行如何在每一行上获取 DIV。

When in doubt, simply add <br/> elements.如有疑问,只需添加<br/>元素即可。

I figured out what to do to solve the problem.我想出了解决问题的方法。 Instead of putting the text in a <p> tag, I put it in a <div> tag in the HTML document and set the display to block and it is working like I want it to.我没有将文本放在 <p> 标记中,而是将它放在 HTML 文档中的 <div> 标记中,并将显示设置为阻塞,它按我想要的方式工作。 I also appended a <br> tag to the end of the text to be added to the JEditorPane.我还在要添加到 JEditorPane 的文本末尾附加了一个 <br> 标记。

For example I changed:例如我改变了:

<p id="look-bed">
    The bed looks hard and uncomfortable.
</p>

to:到:

<div id="look-bed">
    The bed looks hard and uncomfortable.
</div>

I added a CSS style specific to the div tag to set the display:我在div标签中添加了一个CSS样式来设置显示:

div { display: block } div { 显示:块 }

The new method that adds text to the JEditorPane is:将文本添加到 JEditorPane 的新方法是:

public static void SetGameText(String location, String text, boolean appendText) {
        if(appendText) {
            try {
                String addedText = text + "<br>"; // added new variable to append <br> tag
                HTMLDocument doc = (HTMLDocument) gameText.getDocument();
                doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), addedText); //use new variable to add text to JEditorPane
            }
            catch (IOException | BadLocationException e) {
                e.printStackTrace();
            }
        }
        else {
            gameText.setText(HTMLStyles);
            gameText.setText(location + text);
        }
    }

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

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