Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JTextPane中为Java关键字着色_Java_Swing_Code Formatting_Jtextpane_Styledtext - Fatal编程技术网

在JTextPane中为Java关键字着色

在JTextPane中为Java关键字着色,java,swing,code-formatting,jtextpane,styledtext,Java,Swing,Code Formatting,Jtextpane,Styledtext,我正在尝试使用JTextPane准备一个Java编辑器 为此,我首先搜索关键字的索引,然后对其进行操作。 我的代码在这里,我使用NetBeans来制作GUI,所以我删除了这些代码 主代码位于replace jTextPane方法中 代码会运行一段时间,但随着行数的增加,会出现问题。行数增加时会发生什么?会出现问题。到底出了什么问题?屏幕截图可能会有所帮助。您没有用于嵌入图像的代表,但您可以将其上载到图像共享网站,并且link.keyword选择不正确。因此,关键字开始重叠我编辑了您的示例以使其独

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

为此,我首先搜索关键字的索引,然后对其进行操作。 我的代码在这里,我使用NetBeans来制作GUI,所以我删除了这些代码

主代码位于replace jTextPane方法中


代码会运行一段时间,但随着行数的增加,会出现问题。

行数增加时会发生什么?会出现问题。到底出了什么问题?屏幕截图可能会有所帮助。您没有用于嵌入图像的代表,但您可以将其上载到图像共享网站,并且link.keyword选择不正确。因此,关键字开始重叠我编辑了您的示例以使其独立。它在键入时工作正常,但在粘贴时失败。我会去看看DoTuntListNeor而不是KeyListener。你是什么意思?高拉夫德什穆克
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
}