📄 intelligenceedit.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.w3c.dom.Text;
public class IntelligenceEdit implements KeyListener{
JTextPane text = new JTextPane(); //信息显示,信息可以以不同的字体和颜色显示.
DefaultStyledDocument document = new DefaultStyledDocument(); //将被设置为JTextPane的关联文档
SimpleAttributeSet attrSet = new SimpleAttributeSet(); //将不同的属性值与此关联,如颜色,字体大小等.
String keyWord[] ={"select","from","where"};
// Vector keyWord = new Vector();//关键字
JFrame frame = new JFrame("编辑主窗口");
public IntelligenceEdit() {}
public void window(){
frame.setVisible(true);
frame.setBounds(10,10,600,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
FileInputStream in = new FileInputStream("build.xml");
text.read(in,"");
in.close();
} catch (FileNotFoundException ex) {} catch (IOException ex) {}
//text.setStyledDocument(document); 不要使用此,否则无法显示导入的数据.
document = (DefaultStyledDocument)text.getStyledDocument(); //使用此方法取得一个文档
//document.addDocumentListener(this); //添加文档监听,不要使用此,会出现异常,不稳定
text.addKeyListener(this);
text.setVisible(true);
frame.add(new JScrollPane(text),BorderLayout.CENTER);
}
public static void main(String args[]){
//StringTokenizer st = new StringTokenizer("this is a test");
//while (st.hasMoreTokens()) {System.out.println(st.nextToken());}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {} catch (UnsupportedLookAndFeelException ex) {} catch (InstantiationException ex) {} catch (IllegalAccessException ex) {}
new IntelligenceEdit().window();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e) {
setStyle();
}
public void setStyle(){
String content = text.getText();
char ch;
String data="";
for(int i=0;i<content.length();i++){
ch = content.charAt(i);
if((ch != ' ')&&(ch != '\n')){
data = data+String.valueOf(ch);
}else{
document.setCharacterAttributes(i-data.length()-1,data.length()+1,getBlank(),false);
for(int j=0;j<keyWord.length;j++){
if(data.equals(keyWord[j])){
document.setCharacterAttributes(i-data.length()-1,data.length()+1,getBlueBlod(),false);
data = "";
}
}
data = "";
}
}
}
public SimpleAttributeSet getBlueBlod(){
StyleConstants.setForeground(attrSet,new Color(16,52,160));//设置蓝色
StyleConstants.setBold(attrSet,true); //设置蓝色为粗体
StyleConstants.setItalic(attrSet,false);
return attrSet;
}
public SimpleAttributeSet getBlank(){
StyleConstants.setForeground(attrSet,Color.BLACK); //设置黑色
StyleConstants.setBold(attrSet,false); //设置黑色为非粗体
StyleConstants.setItalic(attrSet,false);
return attrSet;
}
public SimpleAttributeSet getRed(){
StyleConstants.setForeground(attrSet,Color.RED); //SimpleAttributeSet设置对应的值.
StyleConstants.setBold(attrSet,false);
StyleConstants.setItalic(attrSet,true);
return attrSet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -