top
Loading...
如何更好的使用JTextPane
我經常在網上看見許多朋友問,如何在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。

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗