我经常在网上看见许多朋友问,如何在jtextarea中控制字符,如何设置特定字符的颜色等等。我在用java做一个sql查询分析器中发现了一个比较好的解决方案就是使用jtextpane,那么如何更好的使用jtextpane呢,我现摘自我那部分程序的一部分,供大家参考。
| package com.jdagui; import javax.swing.text.*; import java.util.*; import java.awt.*; import com.jda.*; /** *@author whxu */ public class jdastyleddocument extends defaultstyleddocument { private int type = -1;//数据连接类型 attributeset myattributeset = null; public jdastyleddocument(int type) { this.type = type; } /** *插入字符串 */ public void insertstring(int offset,string str,attributeset a) throws badlocationexception { this.myattributeset = a; super.insertstring(offset,str,a); setsyntaxcolor(offset,str.length()); } /** *删除字符串 */ public void remove(int offs,int len) throws badlocationexception { super.remove(offs,len); setsyntaxcolor(offs); } /** *获取制定位置的字符 */ private string getpositionchar(int offset) { string str = ""; try { str = gettext(offset,1); } catch(badlocationexception ex) { //ex.printstacktrace(system.out); } return str; } /** *从指定的位置开始,倒推到第一个遇到空格位置 */ private string getbeforeblankstring(int offset) { string str = ""; if(offset<0) return ""; str = getpositionchar(offset); if(syntaxmgr.isspacechar(str)) return ""; string r = getbeforeblankstring(offset-1); return r + str; } /** *从指定的位置开始,顺推到第一个遇到空格位置 */ private string getafterblankstring(int offset) { string str = ""; if(offset>getlength()) return ""; str = getpositionchar(offset); if(syntaxmgr.isspacechar(str)) return ""; string r = getafterblankstring(offset+1); return str + r; } /** * 根据postion,向前判断,向后判断,设置颜色,返回设置颜色末尾的位置 */ private int setsyntaxcolor(int offset) { if(offset<0) return offset;//如果设置的位置不存在,可以不用考虑 if(myattributeset==null) return offset;//如果myattributeset为null,可以不用考虑 string ifsyntax = ""; string before = getbeforeblankstring(offset-1); string after = getafterblankstring(offset); syntax = (before + after).trim(); int start = offset-before.length(); int tmp_len = ifsyntax.length(); if(start<0 || tmp_len<=0) return offset;//如果设置颜色的字符串为空,返回 //设置颜色 styleconstants.setforeground((mutableattributeset)myattributeset, syntaxmgr.issyntax(type,ifsyntax)); setcharacterattributes(start,tmp_len,myattributeset,true); return start + tmp_len; } /** *根据一个范围,设置该范围内的的syntaxcolor */ private int setsyntaxcolor(int offset,int len) throws badlocationexception { //如果范围不存在,不考虑 if(offset<0 || len<0) return offset; int tmp_offset = offset; while(tmp_offset<offset + len) { tmp_offset = setsyntaxcolor(tmp_offset); tmp_offset = getnextwordoffset(tmp_offset); } tmp_offset = setsyntaxcolor(tmp_offset);//设置循环完后的最后一个单词 return tmp_offset; } /** *根据postion,获得下一个单词的开始点 */ private int getnextwordoffset(int offset) { int roffset = offset; int textlength = getlength(); while(roffset<=textlength && offset>=0) { string str = getpositionchar(roffset); if(!syntaxmgr.isspacechar(str)) { break; } roffset+=1; } if(roffset!=offset)//设置间隔的颜色 { //设置颜色 styleconstants.setforeground((mutableattributeset)myattributeset, syntaxcolormgr.getspacecolor()); setcharacterattributes(offset,roffset-offset,myattributeset,true); } return roffset; } } |
到此为此,我们做好了一个适用于jtextpane的document。使用jtextpane就比较简单了。
可以这样使用
jtextpane sqlpane = new jtextpane(new jdastyleddocument(type));
因为我这个例子中使用了根据数据库类型不同来显示不同的关键字,所以我传了一个int type。
闽公网安备 35060202000074号