是的,我有一个记事,当用户在里边写一些东西时,当用户输入一此java 的关键字时,就要对这些关键字作一些特殊的显示,就是编译器里一样的
解决方案 »
- 在一个javaweb项目里能不能建2个webapp这样的文件夹啊
- Spring+JPA+Hibernate+Oracle偶尔出现数据提交不成功的问题
- ext动态树的问题。我菜的很。
- spring jmx InvalidInvocationException异常,达人帮忙看看
- struts1.2+hibernate3+spring2.0
- Spring整和Hibernate启动后的问题
- 我想在web页上面安插一个类似于word的文本编辑器,哪位大侠有这个控件呀??急急急!!!!!!!!!!!!
- struct1.2下的Mybean中咋获取客户的remote_ip??
- 请问有人用过RemotelyAnywhere远程控制软件吗?
- jbuilder工程中添加java文件 只能一个一个填而且没有目录结构?
- struts 写数据库出错
- LinkedList对象的removeLast()方法是什么意思????
你可以将关键字放到一个ArrayList中,在输入单词时判断是否与关键字匹配,单词是以空格和各种符号(如=,+-(等)分隔
也可以参考一下别人的编辑器
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;class MyEditorKit extends DefaultEditorKit
{
public MyEditorKit(){
super();
} public ViewFactory getViewFactory(){
return new MyViewFactory();
}
}class MyViewFactory implements ViewFactory
{
public MyViewFactory(){
} public View create(Element element){
return new MyEditorView(element);
}
}class MyEditorView extends PlainView implements MouseMotionListener
{
boolean isDragging=false;
public MyEditorView(Element element){
super(element);
} protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1)
throws BadLocationException{
if(isDragging) return x; Document doc=getDocument();
Segment segment=new Segment(), token=new Segment();
int index=0, count=p1-p0;
char c='\u0000'; doc.getText(p0, count, segment);
for(int i=0; i<count; i++){
if(Character.isLetter(c=segment.array[segment.offset+i])||c=='_'){ // :)
index=i;
while(++i<count&&(Character.isLetter((c=segment.array[segment.offset+i]))||c=='_'||(c>='0'&&c<='9')));
doc.getText(p0+index, (i--)-index, token);
if(KeyWord.isKeyWord(token)){
// g.setFont(KEYWORDFONT);
g.setColor(KEYWORDCOLOR);
}else{
g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
}
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}else if(c>='0'&&c<='9'){
index=i;
while(++i<count&&(c=segment.array[segment.offset+i])>='0'&&c<='9');
doc.getText(p0+index, (i--)-index, token);
g.setColor(NUMCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}else if(c=='/'){
index=i;
if(++i<count&&segment.array[segment.offset+i]=='/'){
doc.getText(p0+index, count-index, token);
g.setFont(COMMENTFONT);
g.setColor(COMMENTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
break;
}
doc.getText(p0+index, 1, token);
g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
i--;
continue;
}else if(c=='\''||c=='\"'){
index=i;
char ch='\u0000';
while(++i<count){
if((ch=segment.array[segment.offset+i])=='\\'){
i++;
continue;
}else if(ch==c) break;
}
if(i>=count) i=count-1;
doc.getText(p0+index, i-index+1, token);
g.setColor(Color.yellow);
g.fillRect(x, y-8, 4, 12);
g.setFont(STRINGFONT);
g.setColor(STRINGCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
continue;
}else{
index=i;
while(++i<count&&!Character.isLetter((c=segment.array[segment.offset+i]))&&c!='_'&&c!='/'&&c!='\''&&c!='\"'&&(c<'0'||c>'9'));
doc.getText(p0+index, (i--)-index, token);
g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR);
x=Utilities.drawTabbedText(token, x, y, g, this, p0+index);
}
}
return x;
} protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
throws BadLocationException{
g.setFont(TEXTFONT);
g.setColor(TEXTCOLOR); isDragging=(p0-p1==0); return super.drawSelectedText(g, x, y, p0, p1);
} public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){
} public static Font TEXTFONT=new Font("DialogInput", Font.PLAIN, 11);
public static Color TEXTCOLOR=Color.black;
public static Font NUMFONT=new Font("DialogInput", Font.PLAIN, 11);
public static Color NUMCOLOR=Color.blue;
public static Font KEYWORDFONT=new Font(TEXTFONT.getFontName(), Font.BOLD, TEXTFONT.getSize());
public static Color KEYWORDCOLOR=new Color(0, 0, 128);
public static Font COMMENTFONT=TEXTFONT;
public static Color COMMENTCOLOR=new Color(192, 192, 192);
public static Font STRINGFONT=TEXTFONT;
public static Color STRINGCOLOR=new Color(255, 0, 0);
}class KeyWord
{
public KeyWord(){
} public static boolean isKeyWord(Segment seg){
boolean isKey=false;
for(int i=0; !isKey&&i<KEYWORDS.length; i++)
if(seg.count==KEYWORDS[i].length()){
isKey=true;
for(int j=0; isKey&&j<seg.count; j++)
if(seg.array[seg.offset+j]!=KEYWORDS[i].charAt(j))
isKey=false; }
return isKey;
} public static final String[] KEYWORDS={
"abstract",
"boolean", "break", "byte",
"case", "catch", "char", "class", "const", "continue",
"default", "do", "double",
"else", "extends",
"final", "finally", "float", "for",
"goto",
"if", "implements", "import", "instanceof", "int", "interface",
"long",
"native", "new",
"package",
"private", "protected", "public",
"return",
"short", "static", "strictfp", "super", "switch", "synchronized",
"this", "throw", "throws", "transient", "try",
"void", "volatile",
"while",
"true", "false"
};
}public class Editor extends JFrame
{
Container c;
JEditorPane editor=new JEditorPane();
MyEditorKit kit=new MyEditorKit(); public Editor(){
super("Editor");
initFrame();
setSize(640, 480);
} private void initFrame(){
c=getContentPane();
c.setLayout(new BorderLayout());
editor.setFont(new Font("DialogInput", Font.PLAIN, 11));
editor.setEditorKitForContentType("text/java", kit);
editor.setContentType("text/java"); editor.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e){
}
});
c.add(new JScrollPane(editor), BorderLayout.CENTER);
} public static void main(String[] args){
Editor App=new Editor();
App.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
App.show();
}
}