Java 特定关键字上的语法高亮显示

Java 特定关键字上的语法高亮显示,java,swing,editor,jtextpane,Java,Swing,Editor,Jtextpane,我正在尝试做一个小编辑器,我需要一些特定关键字的语法突出显示。我希望你有耐心看完这篇文章。我只想突出显示JTextPane中.txt文件中的单词。我需要一个方法来解决这个问题,希望你能给我指出正确的方向 我首先创建了这个.txt文件: if else echo return global for 在此处读取.txt文件并进行一些检查: import javax.swing.text.DefaultStyledDocument; import java.io.File; import java.

我正在尝试做一个小编辑器,我需要一些特定关键字的语法突出显示。我希望你有耐心看完这篇文章。我只想突出显示JTextPane中.txt文件中的单词。我需要一个方法来解决这个问题,希望你能给我指出正确的方向

我首先创建了这个.txt文件:

if
else
echo
return
global
for
在此处读取.txt文件并进行一些检查:

import javax.swing.text.DefaultStyledDocument;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Created by Ilhami on 22-11-2014.
 */
public class SyntaxHighlighter {
    final static Charset ENCODING = StandardCharsets.UTF_8;
    private List<String> lines;

    public SyntaxHighlighter() {
        try {
            lines = readFile("keywords.txt");
        } catch(IOException e) {
            e.getStackTrace();
        }
    }

    public boolean keywordMatch(List<String> lines, String word) throws IOException {
        boolean isTrue = false;
        for(String item : lines) {
            if(!item.isEmpty() && !word.isEmpty()) {
                if(item.equals(word)) {
                    return isTrue = true;
                }
            }
        }
        return isTrue;
    }

    public List<String> readFile(String file) throws IOException {
        Path path = Paths.get(file);
        return Files.readAllLines(path, ENCODING);
    }

    public List<String> getLines() {
        return this.lines;
    }
}
这是我的编辑器类,我在其中得到错误:

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.io.IOException;
import java.util.List;

/**
 * Created by Ilhami on 22-11-2014.
 */
public class Editor {
    private JFrame editor;
    public JTextPane editorPane;
    private SyntaxHighlighter syntaxHighlighter;
    private List<String> lines;
    private JScrollPane scrollPane;

    public Editor() {
        editor = new JFrame("Editor");
        editorPane = new JTextPane();
        editor.setLayout(new BorderLayout());
        editor.setLocationRelativeTo(null);
        scrollPane = new JScrollPane(editorPane);

        syntaxHighlighter = new SyntaxHighlighter();
        lines = syntaxHighlighter.getLines();

        editorPane.getDocument().addDocumentListener(new MyDocumentListener());

    }

    public void showEditor() {
        editor.add(scrollPane);
        editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        editor.setPreferredSize(new Dimension(500, 500));
        editor.setSize(new Dimension(500, 500));
        editor.setVisible(true);
    }

    class MyDocumentListener implements DocumentListener {
        private StyledDocument document;

        Style style, style2;
        public MyDocumentListener() {
            document = editorPane.getStyledDocument();
            editorPane.setDocument(document);
        }

        private void appendToPane(JTextPane tp, String msg, Color c, int len)
        {
            StyleContext sc = StyleContext.getDefaultStyleContext();
            AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

            aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
            aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

            len = tp.getDocument().getLength();
            tp.setCaretPosition(len);
            tp.setCharacterAttributes(aset, false);
            tp.replaceSelection(msg);
        }

        public void setFont() {
            try {
                for (String string : editorPane.getText().split("\\s+")) {
                    if (syntaxHighlighter.keywordMatch(lines, string)) {
                        appendToPane(editorPane, string, new Color(12, 146, 20), 40);
                    } else {
                        appendToPane(editorPane, string, new Color(22, 22, 22), 40);
                    }
                }
            } catch (IOException ex) {
                ex.getStackTrace();
            }
        }


        @Override
        public void insertUpdate(DocumentEvent e) {
            setFont();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setFont();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            //System.out.println("Hej");
        }
    }
}

改用文档过滤器让我试试这个,看看它是否有效。也许类似
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.io.IOException;
import java.util.List;

/**
 * Created by Ilhami on 22-11-2014.
 */
public class Editor {
    private JFrame editor;
    public JTextPane editorPane;
    private SyntaxHighlighter syntaxHighlighter;
    private List<String> lines;
    private JScrollPane scrollPane;

    public Editor() {
        editor = new JFrame("Editor");
        editorPane = new JTextPane();
        editor.setLayout(new BorderLayout());
        editor.setLocationRelativeTo(null);
        scrollPane = new JScrollPane(editorPane);

        syntaxHighlighter = new SyntaxHighlighter();
        lines = syntaxHighlighter.getLines();

        editorPane.getDocument().addDocumentListener(new MyDocumentListener());

    }

    public void showEditor() {
        editor.add(scrollPane);
        editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        editor.setPreferredSize(new Dimension(500, 500));
        editor.setSize(new Dimension(500, 500));
        editor.setVisible(true);
    }

    class MyDocumentListener implements DocumentListener {
        private StyledDocument document;

        Style style, style2;
        public MyDocumentListener() {
            document = editorPane.getStyledDocument();
            editorPane.setDocument(document);
        }

        private void appendToPane(JTextPane tp, String msg, Color c, int len)
        {
            StyleContext sc = StyleContext.getDefaultStyleContext();
            AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

            aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
            aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

            len = tp.getDocument().getLength();
            tp.setCaretPosition(len);
            tp.setCharacterAttributes(aset, false);
            tp.replaceSelection(msg);
        }

        public void setFont() {
            try {
                for (String string : editorPane.getText().split("\\s+")) {
                    if (syntaxHighlighter.keywordMatch(lines, string)) {
                        appendToPane(editorPane, string, new Color(12, 146, 20), 40);
                    } else {
                        appendToPane(editorPane, string, new Color(22, 22, 22), 40);
                    }
                }
            } catch (IOException ex) {
                ex.getStackTrace();
            }
        }


        @Override
        public void insertUpdate(DocumentEvent e) {
            setFont();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            setFont();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            //System.out.println("Hej");
        }
    }
}
public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Editor editor = new Editor();
                editor.showEditor();
            }
        });
    }
}