【发布时间】:2016-03-02 22:20:31
【问题描述】:
我想在突出显示时从两点更改我的 Swing 应用程序 (JTextPane) 的文本颜色。如果用户突出显示索引 4 到 9 中的短语,那么只有这些字符会永久更改其颜色。我说永久是因为我已经知道有一个setSelectionColor() 选项,但这只是暂时的。我已经设法获得了突出显示文本的起点和终点,但我走到了死胡同。
这是我目前所拥有的:
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet attributes = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);
if(tp.getSelectedText() != null){//tp is a jtextpane. text is highlighted. change highlighted text color
int start = tp.getSelectionStart();
int end = tp.getSelectionEnd();
//update the color of the text within start and end
}
tp.setCharacterAttributes(attributes, false);//update the color for the new text
【问题讨论】:
标签: java swing colors highlight