另一种方式处理JTextPane中的关键字显示
mport java.awt.Color;
import java.util.StringTokenizer;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.rtf.RTFEditorKit;
/**
*@author gao
*/
/**
* @author Administrator
*
*/
/**
* @author Administrator
*
*/
public class CustomizeStyledDocument extends DefaultStyledDocument {
private int type = -1;// 数据连接类型
AttributeSet myAttributeSet = null;
private MutableAttributeSet keyAttr, normalAttr;
private MutableAttributeSet inputAttributes = new RTFEditorKit()
.getInputAttributes();
private String[] keyWord = { "int", "float" };
public CustomizeStyledDocument(int type) {
this.type = type;
// 定义关键字显示属性
keyAttr = new SimpleAttributeSet();
StyleConstants.setForeground(keyAttr, Color.red);
// 定义一般文本显示属性
normalAttr = new SimpleAttributeSet();
StyleConstants.setForeground(normalAttr, Color.black);
}
/**
*插入字符串
*/
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);
Element root = getDefaultRootElement();
int linebeg = root.getElementIndex(offs);
dealLine(root,linebeg);
}
/**
*根据一个范围,设置该范围内的的SyntaxColor
*/
private void setSyntaxColor(int offset, int len)
throws BadLocationException {
Element root = getDefaultRootElement();
int linebeg = root.getElementIndex(offset);
int lineEnd = root.getElementIndex(offset + len);
for(int i=linebeg;i<lineEnd+1;i++){
dealLine(root,i);
}
}
/**
* 处理一行中的Word
*/
private void dealLine(Element root,int curLine) {
Element para = root.getElement(curLine);
int start = para.getStartOffset();
int end = para.getEndOffset() - 1;
String s=null;
try {
s = getText(start, end - start);
} catch (BadLocationException e) {
e.printStackTrace();
return;
}
int i = 0;
int xStart = 0;
// 分析关键字---
setCharacterAttributes(start, s.length(), normalAttr, false);
MyStringTokenizer st = new MyStringTokenizer(s);
while (st.hasMoreTokens()) {
s = st.nextToken();
if (s == null)
return;
for (i = 0; i < keyWord.length; i++) {
if (s.equals(keyWord[i]))
break;
}
if (i >= keyWord.length)
continue;
xStart = st.getCurrPosition();
// 设置关键字显示属性
setCharacterAttributes(start + xStart, s.length(), keyAttr, false);
}
inputAttributes.addAttributes(normalAttr);
}
}
/**
* 拆分一行中的word
*
*/
class MyStringTokenizer extends StringTokenizer {
String sval = " ";
String oldStr, str;
int m_currPosition = 0, m_beginPosition = 0;
MyStringTokenizer(String str) {
super(str, " ");
this.oldStr = str;
this.str = str;
}
public String nextToken() {
try {
String s = super.nextToken();
int pos = -1;
if (oldStr.equals(s)) {
return s;
}
pos = str.indexOf(s + sval);
if (pos == -1) {
pos = str.indexOf(sval + s);
if (pos == -1)
return null;
else
pos += 1;
}
int xBegin = pos + s.length();
str = str.substring(xBegin);
m_currPosition = m_beginPosition + pos;
m_beginPosition = m_beginPosition + xBegin;
return s;
} catch (java.util.NoSuchElementException ex) {
ex.printStackTrace();
return null;
}
}
// 返回token在字符串中的位置
public int getCurrPosition() {
return m_currPosition;
}
}
同样,使用时作为JTextPane中的Document即可
如果要可以识别“// “种类别的注释,可以加上以下内容
int pos = s.indexOf("//");
if(pos != -1){
setCharacterAttributes(pos+start, s.length()-pos, commentiAttr, true);
if(pos==0)
return;
s=s.substring(0, pos);
}
其中commentiAttr为自定义的注释的风格。
分享到:
相关推荐
SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
JAVA-----JTextPane实例 代码
JTextPane使用,可以传图片,文字等
演示、说明程序 博文链接:https://bps.iteye.com/blog/136627
NULL 博文链接:https://icarusliu.iteye.com/blog/1412007
课程设计期间做的Java的桌面端小程序,可以发送Emoji表情的聊天室,另外在文件夹中放置了自己的关键代码部分解析,关于emoji部分可查看笔者博客...
本源码实现了 文本区里加图片和插入艺术字的功能 窗口使用Windows窗体风格
NULL 博文链接:https://icarusliu.iteye.com/blog/2098986
Java制作的网页浏览器和源代码查看器,仿IE的网页浏览功能,不支持HTML5,窗口采用上下分屏显示的方式,上面显示网页浏览器,下面显示该网页的源代码,JTextField jtfAddress; //输入html文件地址或网址 ...
JTextPane jtpShow; //显示文件 JLabel jlInfo; //提示信息 public HTTPBrowserDemo() { super("韩公达制作的html浏览器"); //调用父类构造函数 jtfAddress=new JTextField(20); //实例化地址输入框 jbGo...
老师给的例子,实现了字体设置。使用了JTextPane,主要还是一个实现设置字体的例子
NULL 博文链接:https://gh0769.iteye.com/blog/598839
JTextPane jtpShow; //显示文件 JLabel jlInfo; //提示信息 public HTTPBrowserDemo(){ super("html浏览器"); //调用父类构造函数 jtfAddress=new JTextField(20); //实例化地址输入框 jbGo=new...
用JTextPanel实现文本带下划线功能