我有一个包含JTextPane的JFrame.这个JTextPane的目的是在我输入单词时突出显示单词,这与程序员的文本编辑器一致.为了实现这一点,我扩展了JTextPane,我实现了KeyListener接口,并将其设置为自我的关键监听器.执行一些重要工作的方法是keyReleased.问题是,我可以突出显示我输入的第一个单词,但在此之后,我继续得到BadLocation,即使开始和结束都在文档限制内.我发布了一些代码片段:
// this is my highlight method private void highlight(int start,int end) throws BadLocationException { Document doc = getDocument(); Color c = Color.red; String text = doc.getText(start,end); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); setCharacterAttributes(aset, true); setSelectionStart(start); setSelectionEnd(end); replaceSelection(text); } //this is my keyReleased method public void keyReleased(KeyEvent arg0) { char character = arg0.getKeyChar(); if(wordStarted) { // have I started typing a new word ? if(character == ' ') { // end word try { int dot = getCaret().getDot(); highlight(wordStart, dot - 1); setCaretPosition(dot); wordStarted = false; } catch (BadLocationException ex) { ex.printStackTrace(); } } } else { if(Character.isLetter(character)) { wordStarted = true; wordStart = getCaret().getDot() -1; } } }
我尝试输入:public static但只有public是红色的.我甚至添加了一些用于调试的println语句,这是输出:
this is outputted after writing public Start param:0 End param:6 Document Length:7 Document START:0 Document END:8 text:public this is outputted after writing static Start param:7 End param:13 Document Length:14 Document START:0 Document END:15 text:public static javax.swing.text.BadLocationException: Invalid location at javax.swing.text.GapContent.getChars(GapContent.java:189) at javax.swing.text.GapContent.getString(GapContent.java:167) at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:774) at ifirst.visual.CodePanel.highlight(CodePanel.java:49) at ifirst.visual.CodePanel.keyReleased(CodePanel.java:82) at java.awt.Component.processKeyEvent(Component.java:6069) at javax.swing.JComponent.processKeyEvent(JComponent.java:2810) at java.awt.Component.processEvent(Component.java:5885) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4469) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1881) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:742) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1007) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:879) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:706) at java.awt.Component.dispatchEventImpl(Component.java:4339) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4295) at java.awt.EventQueue.dispatchEvent(EventQueue.java:604) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
我正在编写此代码以获取JTextPane的挂起.我对像JTextPane这样的东西不感兴趣.
正如您所发现的,Document.getText()采用开始和长度,而不是开始和结束参数.我想我会提供一个答案和一个链接到JavaDoc,供其他人阅读你的问题,他们可能不会在评论中查看.