gpt4 book ai didi

java - JEdi​​torPane 中的换行符

转载 作者:行者123 更新时间:2023-11-30 11:47:01 25 4
gpt4 key购买 nike

我目前正在使用 JEditorPane 来显示从 Socket 读取的文本。我创建了 JEditorPane,获取它的文档并使用

添加文本
document.insertString(document.getEndPosition().getOffset(), string, null); 

这按预期工作,除了当它遇到 '\n'(用于换行符的字符)时,它似乎忽略它(即不打印任何内容),并且所有内容都在一行中出现。我四处寻找解决方案,但似乎找不到任何有效的方法(尝试用 <br> 替换 '\n',没有帮助,它只是打印出 <br>

最佳答案

它如你所料的那样工作。

这是代码。

doc.insertString(doc.getEndPosition().getOffset(), "test\n", null);
doc.insertString(doc.getEndPosition().getOffset(), "test\n", null);
doc.insertString(doc.getEndPosition().getOffset(), "test\n", null);
doc.insertString(doc.getEndPosition().getOffset(), "test\n", null);

完整代码。

    jScrollPane1 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(jEditorPane1);

getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

pack();
  1. 检查是否已在编辑器 Pane 上设置任何其他属性。
  2. 您使用的是什么编辑器工具包?
  3. 尝试打印输入并确保它带有'\n'

你也可以使用,

    doc.insertString(doc.getLength(), "test\n", null);

关于java - JEdi​​torPane 中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com