gpt4 book ai didi

java - 如何删除 JTextPane 中的第一行并设置 setContentType ("text/html")

转载 作者:行者123 更新时间:2023-12-01 11:52:28 26 4
gpt4 key购买 nike

我想删除 JTextPane 中的第一行并设置 setContentType("text/html");能够在 JTextPane 中使用 html 标签。

当我使用此代码时

setContentType("text/html");

try {
Element root = getDocument().getDefaultRootElement();
Element firstLine = root.getElement(0);
getDocument().remove(firstLine.getStartOffset(), firstLine.getEndOffset());
}
catch (Exception e) {e.toString();}

我遇到异常

javax.swing.text.BadLocationException: Invalid remove

如果我评论了 setContentType("text/html");我可以从 JTextPane 中删除第一行。

最佳答案

对于 HTMLEditorKit Element firstLine = root.getElement(0);通常返回 <HEAD>但你需要<BODY> 。遍历元素树,找到 BODY 并删除 BODY 的第一个子元素。

更新:该链接显示了一个可用于了解文档结构的工具。 http://java-sl.com/JEditorPaneStructureTool.html

您可以检查您拥有哪些元素,并确定应该删除哪个元素。

关于java - 如何删除 JTextPane 中的第一行并设置 setContentType ("text/html"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695547/

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