特定单词don';在JTextPane[java]中不能更改颜色

特定单词don';在JTextPane[java]中不能更改颜色,java,swing,jtextpane,jdk1.6,Java,Swing,Jtextpane,Jdk1.6,我正在编写一个代码,其目标是拥有一个simil文本编辑器,其中一些单词(用下面的代码编写)、键入时更改颜色,或者从JFileChooser中获取的.txt文件加载。 代码如下: package AppPackage; import java.awt.Color; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader;

我正在编写一个代码,其目标是拥有一个simil文本编辑器,其中一些单词(用下面的代码编写)、键入时更改颜色,或者从JFileChooser中获取的.txt文件加载。 代码如下:

package AppPackage;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

/**
 *
 * @author Alessandro
 */
public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        final StyleContext cont = StyleContext.getDefaultStyleContext();
        final AttributeSet attrGray = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.gray);
        final AttributeSet attrGreen = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.GREEN);
        final AttributeSet attrBlue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
        final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
        final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
        DefaultStyledDocument doc = new DefaultStyledDocument() {
            @Override
            public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
                super.insertString(offset, str, a);

                String text = getText(0, getLength());
                int before = findLastNonWordChar(text, offset);
                if (before < 0) before = 0;
                int after = findFirstNonWordChar(text, offset + str.length());
                int wordL = before;
                int wordR = before;

                while (wordR <= after) {
                    if (wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {                     

                        if (text.substring(wordL, wordR).matches("(\\W)*(System)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(Mobile)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(heaven)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(calc)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(private)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(public)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrGray, false);
                        else if (text.substring(wordL, wordR).matches("(\\W)*(party)"))
                            setCharacterAttributes(wordL, wordR - wordL, attrRed, false); 

                        else
                            setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                        wordL = wordR;
                    }
                    wordR++;
                }
            }

            @Override
            public void remove (int offs, int len) throws BadLocationException {
                super.remove(offs, len);

                String text = getText(0, getLength());
                int before = findLastNonWordChar(text, offs);
                if (before < 0) before = 0;
                int after = findFirstNonWordChar(text, offs);

                if (text.substring(before, after).matches("(\\W)*(System)")) 
                    setCharacterAttributes(before, after - before, attrBlue, false);
                else if (text.substring(before, after).matches("(\\W)*(PlasmaMembrane)")) 
                    setCharacterAttributes(before, after - before, attrBlue, false);   
                else if (text.substring(before, after).matches("(\\W)*(Cytosol)")) 
                    setCharacterAttributes(before, after - before, attrBlue, false);   
                else if (text.substring(before, after).matches("(\\W)*(Organelle)")) 
                    setCharacterAttributes(before, after - before, attrBlue, false);   
                else if (text.substring(before, after).matches("(\\W)*(Nucleous)")) 
                    setCharacterAttributes(before, after - before, attrBlue, false);   
                else if (text.substring(before, after).matches("(\\W)*(volume)")) 
                    setCharacterAttributes(before, after - before, attrGray, false);
                else if (text.substring(before, after).matches("(\\W)*(rate)")) 
                    setCharacterAttributes(before, after - before, attrRed, false);   
                 else {
                    setCharacterAttributes(before, after - before, attrBlack, false);
                }
            } 
        }; 
        JTextPane txt = new JTextPane(doc);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        openBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        textModel = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        openBtn.setText("Open");
        openBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openBtnActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(textModel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(216, 216, 216)
                        .addComponent(openBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(openBtn)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
String spec;
File f;
String filename;
JFileChooser chooser;

private void openBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt files", "txt", "text");
    chooser.setFileFilter(filter);
    int option = chooser.showOpenDialog(null);
    if (option == JFileChooser.APPROVE_OPTION) {
    f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    String modelSpec = readSpecification();
    spec = modelSpec; 
    textModel.setText(spec);

    }
}


public String readSpecification() {
        String spec1 = "";

        // trying to read from file the specification...
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filename));
            String line = reader.readLine();
            while(line!=null) {
                spec1 += line + "\n";
                line = reader.readLine();
            }        
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return spec1;
    }

private int findLastNonWordChar (String text, int index) {
        while (--index >= 0) {
            if (String.valueOf(text.charAt(index)).matches("\\W")) {
                break;
            }
        }
        return index;
    }

    private int findFirstNonWordChar (String text, int index) {
        while (index < text.length()) {
            if (String.valueOf(text.charAt(index)).matches("\\W")) {
                break;
            }
            index++;
        }
        return index;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
                new NewJFrame();
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton openBtn;
    private javax.swing.JTextPane textModel;
    // End of variables declaration
}
package-AppPackage;
导入java.awt.Color;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入javax.swing.JFileChooser;
导入javax.swing.JTextPane;
导入javax.swing.filechooser.FileNameExtensionFilter;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DefaultStyledDocument;
导入javax.swing.text.StyleConstants;
导入javax.swing.text.StyleContext;
/**
*
*@作者亚历山德罗
*/
公共类NewJFrame扩展了javax.swing.JFrame{
/**创建新表单NewJFrame*/
公共NewJFrame(){
初始化组件();
final StyleContext cont=StyleContext.getDefaultStyleContext();
最终属性set attrGray=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.gray);
最终属性set attrGreen=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.GREEN);
最终属性set attrBlue=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLUE);
最终属性set attrRed=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.RED);
最终属性set attrBlack=cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLACK);
DefaultStyledDocument doc=新的DefaultStyledDocument(){
@凌驾
public void insertString(int offset、String str、AttributeSet a)引发BadLocationException{
super.insertString(offset,str,a);
String text=getText(0,getLength());
int before=findLastNonWordChar(文本,偏移量);
如果(之前<0)之前=0;
int after=findFirstNonWordChar(text,offset+str.length());
int-wordL=before;
int-wordR=before;
while(wordR=0){
如果(String.valueOf(text.charAt(index)).matches(“\\W”)){
打破
}
}
收益指数;
}
私有int findFirstNonWordChar(字符串文本,int索引){
while(索引
这是一个SSCCE。 没有出现错误或异常,但是单词没有改变颜色,我不明白为什么。我将应该改变颜色的代码放在构造函数中,并将调用
newnewjframe();
放在main方法中。 你能帮我解决这个问题吗?
谢谢!

差不多了你做得很好。差不多了。 需要对代码进行小的更改:

  • 为方法
    initComponents
    添加参数:

    initComponents(DefaultStyledDocument文档)

  • 将此参数传递给
    JTextPane

    textModel=newjavax.swing.JTextPane(doc)

  • NewJFrame
    构造函数中删除
    JTextPane txt=newjtextpane(doc);

  • 将initComponents移动到
    NewJFrame
    构造函数的末尾

    初始组件(doc)


  • 差不多你做到了,差不多。 需要对代码进行小的更改:

  • 为方法
    initComponents
    添加参数:

    initComponents(DefaultStyledDocument文档)

  • 将此参数传递给
    JTextPane

    textModel=newjavax.swing.JTextPane(doc)

  • NewJFrame
    构造函数中删除
    JTextPane txt=newjtextpane(doc);

  • 将initComponents移动到
    NewJFrame
    构造函数的末尾

    初始组件(doc)