Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14441946
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 21:41:29

下载本文示例代码
  我经常在网上看见许多朋友问,如何在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=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。 zmbbs=1;   我经常在网上看见许多朋友问,如何在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=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。 zmbbs=1; 下载本文示例代码


如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane如何更好的使用JTextPane
阅读(182) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~