在JTextPane中着色Java关键字

时间:2012-08-26 09:34:28

标签: java swing code-formatting jtextpane styledtext

我正在尝试使用JTextPane准备Java编辑器。

为此,我首先搜索关键字索引,然后对其进行操作。 我的代码就在这里(我使用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
}

代码可以工作一段时间,但是没有。线条增加了问题。

0 个答案:

没有答案