我正在尝试使用JTextPane
.
为此,我首先搜索关键字的索引,然后对其进行操作。我的代码在这里(我使用 NetBeans 制作 GUI,所以我删除了该代码)
主要代码在replace (jTextPane)
方法中。
package gad.dag;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.*;
public class Editor extends javax.swing.JFrame {
javax.swing.JTextPane jEditorPane1 = new javax.swing.JTextPane();
int fromIndex = 0;
String[] keywords = {"import ","class ","int ","while","if","for","void","boolean","abstract"
,"byte ","static ","break","char ","try","catch","case","const","continue","default","new ","double "
,"else","enum ","extends ","finally","float ","final ","goto ","implements "
,"instanceof","interface ","long ","native","package ","private "
,"public ","protected ","return","short ","super","strictfp","switch",
"synchronized","this","throw","throws","transient","volatile","assert"};
public Editor() {
//initComponents();
setSize(700,600);
setTitle("Java Editor");
jEditorPane1.addKeyListener(new KeyListener(){
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
jEditorPane1ActionPerformed(e);
}
});
Font font = new Font("Lucida Console",Font.PLAIN,14);
jEditorPane1.setFont(font);
this.add(new javax.swing.JScrollPane(jEditorPane1));
}
public int countLine(String str) {
int n= 0 ;
//char c = '\n';
for(int i=0;i<str.length();i++)
if(str.charAt(i)=='\n') n++;
return n;
}
public void replace(javax.swing.JTextPane jp) {
int cur = jp.getCaretPosition();
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLUE);
for(int i=0;i<keywords.length;i++) {
int fromIndex = 0;
String msg = keywords[i];
int nol = 0; //number of lines upto keyword
int len=1;
while(len!=-1) {
len = jp.getText().indexOf(msg, fromIndex);
jp.setSelectedTextColor(Color.RED);
if(len!=-1) {
try {
nol = countLine(jp.getText(0, len+1));
} catch (BadLocationException ex) {
break;
}
fromIndex = len+msg.length();
System.out.println("len = "+len+" nol="+nol);
len-=nol;
jp.select(len, len+msg.length());
System.out.println("Selected Text = "+jp.getSelectedText());
jp.replaceSelection("");
jp.setCaretPosition(len);
jp.setCharacterAttributes(aset, false);
jp.replaceSelection(msg);
}
}
}
aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
jp.setCharacterAttributes(aset, false);
jp.setCaretPosition(cur);
}
private void jEditorPane1ActionPerformed(KeyEvent evt) {
replace(jEditorPane1);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Editor().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem10;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem jMenuItem7;
private javax.swing.JMenuItem jMenuItem8;
private javax.swing.JMenuItem jMenuItem9;
private javax.swing.JTabbedPane jTabbedPane2;
// End of variables declaration//GEN-END:variables
}
该代码工作了一段时间,但没有。行增加的事情出错了。