javax.swing.text.BadLocationException 尝试从文档中删除文本时

javax.swing.text.BadLocationException when trying to remove text from doc

我正在尝试 remove/delete 两点之间文档中的一些文本。 假设我有一个包含文本“1234XS”的文档,我正在尝试删除索引 4 和 5 之间的文本 "XS"。但是,当我尝试删除它时出现 BadLocationException 错误.这是我拥有的:

System.out.println(tp.getText().length());//tp is a JTextPane. prints out 6, just to show I'm not going out of bounds
System.out.println(position+ "-" + (position+ 1));//prints out 4 and 5
tp.getStyledDocument().remove(position, (position + 1));//crashes here, trying to remove "XS" from "1234XS"
remove(position, position + 1)

remove(...) 方法的参数不是 startend 偏移量。

参数是startlength。所以代码应该是:

remove(position, 1)