Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试从文档中删除文本时发生javax.swing.text.BadLocationException_Java_Swing_Text_Document - Fatal编程技术网

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

尝试从文档中删除文本时发生javax.swing.text.BadLocationException,java,swing,text,document,Java,Swing,Text,Document,我正试图在两点之间从文档中删除一些文本。 假设我有一个文本为“1234XS”的文档,我试图删除文本“XS”,它位于索引4和5之间。然而,当我尝试删除它时,我得到了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

我正试图在两点之间从文档中删除一些文本。 假设我有一个文本为“1234XS”的文档,我试图删除文本“XS”,它位于索引4和5之间。然而,当我尝试删除它时,我得到了
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(…)
方法的参数不是
start
end
偏移量

参数是
开始
长度
。因此,代码应该是:

remove(position, 1)
remove(…)
方法的参数不是
start
end
偏移量

参数是
开始
长度
。因此,代码应该是:

remove(position, 1)

你想从哪里删除这个?在DocumentListener中?如果是,请不要。您要从何处删除此项?在DocumentListener中?如果是这样,不要。