0
我正在使用JTextPane在Java中创建带有语法高亮的文本编辑器。当我运行该程序时,我得到以下输出: https://www.dropbox.com/s/kkce9xvtriujizy/Output.JPG?dl=0JTextPane语法高亮偏移不正确
我希望每个HTML标记都会突出显示为粉红色,但是在几个标记之后它会开始突出显示错误的区域。
这里是高亮代码:
private void htmlHighlight() {
String textToScan;
textToScan = txtrEdit.getText();
StyledDocument doc = txtrEdit.getStyledDocument();
SimpleAttributeSet sas = new SimpleAttributeSet();
while(textToScan.contains(">")) {
StyleConstants.setForeground(sas, new Color(0xEB13B1));
StyleConstants.setBold(sas, true);
doc.setCharacterAttributes(textToScan.indexOf('<'), textToScan.indexOf('>'), sas, false);
StyleConstants.setForeground(sas, Color.BLACK);
StyleConstants.setBold(sas, false);
textToScan = textToScan.substring(textToScan.indexOf('>') + 1, textToScan.length());
}
}
提前感谢!
想这一点,但现在我的新的输出看起来是这样的: – JPadley
https://www.dropbox.com/s/eb0dicxur99cr0w/newOutput.JPG ?dl = 0 – JPadley
@JPadley对,对不起。新的更新。 –