添加相应的监听器: jtextpane.addCaretListener( new CaretListener() { public void caretUpdate( CaretEvent e ) { int start = e.getDot();//选取的左端位置 int end = e.getMark();//选取的右端位置 AttributeSet s = new SimpleAttributeSet(); s.setAttribute( "AttributeSet.ColorAttribute",Color.RED ); StyleDocument doc = jtextpane.getStyleDocument(); doc.setCharacterAttributes( start,dend,s,true ); } } );Just try!!
jtextpane.addCaretListener( new CaretListener() {
public void caretUpdate( CaretEvent e ) {
int start = e.getDot();//选取的左端位置
int end = e.getMark();//选取的右端位置 AttributeSet s = new SimpleAttributeSet();
s.setAttribute( "AttributeSet.ColorAttribute",Color.RED );
StyleDocument doc = jtextpane.getStyleDocument();
doc.setCharacterAttributes( start,dend,s,true );
}
} );Just try!!