我正在尝试从文档中删除/删除两点之间的一些文本。
假设我有一个包含文本“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(...)
方法的参数不是 start
和 end
偏移量。
参数是开始
和长度
。所以代码应该是:
remove(position, 1)
关于尝试从文档中删除文本时出现 javax.swing.text.BadLocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979536/