`
colorlife
  • 浏览: 130529 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JTextPane中词汇的高亮显示

阅读更多

转自:http://chenghy28.iteye.com/blog/547489

 

Java代码 
  1. //一个可作为JTextPane的document的代码  
Java代码 
  1. import javax.swing.text.AttributeSet;  
  2. import javax.swing.text.BadLocationException;  
  3. import javax.swing.text.DefaultStyledDocument;  
  4. import javax.swing.text.MutableAttributeSet;  
  5. import javax.swing.text.StyleConstants;  
  6.   
  7. /** 
  8.  *@author gao 
  9.  
  10.  */  
  11. public class CustomizeStyledDocument extends DefaultStyledDocument {  
  12.  private int type = -1;// 数据连接类型  
  13.   
  14.  AttributeSet myAttributeSet = null;  
  15.   
  16.  public CustomizeStyledDocument(int type) {  
  17.   this.type = type;  
  18.  }  
  19.   
  20.  /** 
  21.   *插入字符串 
  22.   */  
  23.  public void insertString(int offset, String str, AttributeSet a)  
  24.    throws BadLocationException {  
  25.   this.myAttributeSet = a;  
  26.   super.insertString(offset, str, a);  
  27.   setSyntaxColor(offset, str.length());  
  28.  }  
  29.   
  30.  /** 
  31.   *删除字符串 
  32.   */  
  33.  public void remove(int offs, int len) throws BadLocationException {  
  34.   super.remove(offs, len);  
  35.   setSyntaxColor(offs);  
  36.  }  
  37.   
  38.  /** 
  39.   *获取制定位置的字符 
  40.   */  
  41.  private String getPositionChar(int offset) {  
  42.   String str = "";  
  43.   try {  
  44.    str = getText(offset, 1);  
  45.   } catch (BadLocationException ex) {  
  46.    // ex.printStackTrace(System.out);  
  47.   }  
  48.   return str;  
  49.  }  
  50.   
  51.  /** 
  52.   *从指定的位置开始,倒推到第一个遇到空格位置 
  53.   */  
  54.  private String getBeforeBlankString(int offset) {  
  55.   String str = "";  
  56.   if (offset < 0)  
  57.    return "";  
  58.   
  59.   str = getPositionChar(offset);  
  60.   if (SyntaxMgr.isSpaceChar(str))  
  61.    return "";  
  62.   
  63.   String r = getBeforeBlankString(offset - 1);  
  64.   return r + str;  
  65.  }  
  66.   
  67.  /** 
  68.   *从指定的位置开始,顺推到第一个遇到空格位置 
  69.   */  
  70.  private String getAfterBlankString(int offset) {  
  71.   String str = "";  
  72.   if (offset > getLength())  
  73.    return "";  
  74.   str = getPositionChar(offset);  
  75.   if (SyntaxMgr.isSpaceChar(str))  
  76.    return "";  
  77.   String r = getAfterBlankString(offset + 1);  
  78.   return str + r;  
  79.  }  
  80.   
  81.  /** 
  82.   *根据Postion,向前判断,向后判断,设置颜色,返回设置颜色末尾的位置 
  83.   */  
  84.  private int setSyntaxColor(int offset) {  
  85.   if (offset < 0)  
  86.    return offset;// 如果设置的位置不存在,可以不用考虑  
  87.   
  88.   if (myAttributeSet == null)  
  89.    return offset;// 如果myAttributeSet为null,可以不用考虑  
  90.   
  91.   String ifSyntax = "";  
  92.   
  93.   String before = getBeforeBlankString(offset - 1);  
  94.   String after = getAfterBlankString(offset);  
  95.   
  96.   ifSyntax = (before + after).trim();  
  97.   // System.out.println(ifSyntax);  
  98.   int start = offset - before.length();  
  99.   
  100.   int tmp_len = ifSyntax.length();  
  101.   
  102.   if (start < 0 || tmp_len <= 0)  
  103.    return offset;// 如果设置颜色的字符串为空,返回  
  104.   
  105.   // 设置颜色  
  106.   StyleConstants.setForeground((MutableAttributeSet) myAttributeSet,  
  107.     SyntaxMgr.isSyntax(type, ifSyntax));  
  108.   
  109.   setCharacterAttributes(start, tmp_len, myAttributeSet, true);  
  110.   
  111.   return start + tmp_len;  
  112.  }  
  113.   
  114.  /** 
  115.   *根据一个范围,设置该范围内的的SyntaxColor 
  116.   */  
  117.  private int setSyntaxColor(int offset, int len) throws
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics