如何在打开文件时和用户键入时突出显示java中的关键字

因此,我试图突出显示java中存在于文本文件中的关键字,因为用户打开.java文件或写入.java文件。 我想我知道如何判断文件是否属于正确的类型。 但是,我不知道如何更改某些关键字的颜色。 如果有人可以提供帮助那将是很好的,因为现在它非常令人困惑。 我想知道我是否可以以某种方式使用我的替换function。 我试图用我的几种方法试图做到这一点,但它仍然不清楚。 我已经把他的大部分方法和听众拿走了,只是知道他们在那里但是不让它更容易阅读。

import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.DocumentListener; import javax.swing.event.DocumentEvent; import java.util.Scanner; import java.io.*; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultHighlighter; import javax.swing.text.Highlighter; import javax.swing.text.Highlighter.HighlightPainter; import javax.swing.text.JTextComponent; import java.net.URI; import java.util.*; public class MyTextEditor extends JFrame implements ActionListener { private JPanel panel = new JPanel(new BorderLayout()); private JTextArea textArea = new JTextArea(0,0); private static final Color TA_BKGRD_CL = Color.BLACK; private static final Color TA_FRGRD_CL = Color.GREEN; private static final Color TA_CARET_CL = Color.WHITE; private JScrollPane scrollPane; private MenuBar menuBar = new MenuBar(); public MyTextEditor() { this.setSize(750,800); this.setTitle("Zenith"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textArea.setFont(new Font("Consolas", Font.BOLD, 14)); textArea.setForeground(TA_FRGRD_CL); textArea.setBackground(TA_BKGRD_CL); textArea.setCaretColor(TA_CARET_CL); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVisible(true); textArea.add(scrollPane,BorderLayout.EAST); final LineNumberingTextArea lineNTA = new LineNumberingTextArea(textArea); DocumentListener documentListen = new DocumentListener() { public void insertUpdate(DocumentEvent documentEvent) { lineNTA.updateLineNumbers(); } public void removeUpdate(DocumentEvent documentEvent) { lineNTA.updateLineNumbers(); } public void changedUpdate(DocumentEvent documentEvent) { lineNTA.updateLineNumbers(); } }; textArea.getDocument().addDocumentListener(documentListen); // Line numbers lineNTA.setBackground(Color.BLACK); lineNTA.setForeground(Color.WHITE); lineNTA.setFont(new Font("Consolas", Font.BOLD, 13)); lineNTA.setEditable(false); lineNTA.setVisible(true); textArea.add(lineNTA); scrollPane.setVisible(true); scrollPane.add(textArea); getContentPane().add(scrollPane); } public void findKeyWords(String ext) { ArrayList wordsInTA = new ArrayList(); int index = 0; if(ext == "java") { for(String line : textArea.getText().split(" ")) { wordsInTA.add(line); index++; } try { while(index>0) { String temp = wordsInTA.get(index); boolean isKeyWord = binarySearch(temp); if(isKeyWord) { //Code that has not yet been made } index--; } } catch(IOException ex) { ex.printStackTrace(); } } } private ArrayList loadJavaWords() throws FileNotFoundException { ArrayList javaWords = new ArrayList(); Scanner scan = new Scanner(new File("JavaKeyWords.txt")); while(scan.hasNext()) { javaWords.add(scan.next()); } scan.close(); return javaWords; } private boolean binarySearch(String word) throws FileNotFoundException { ArrayList javaWords = loadJavaWords(); int min = 0; int max = javaWords.size()-1; while(min  0) { min = index +1; } else if(result < 0) { max = index -1; } } return false; } public void replace() { String wordToSearch = JOptionPane.showInputDialog(null, "Word to replace:"); String wordToReplace = JOptionPane.showInputDialog(null, "Replacement word:"); int m; int total = 0; int wordLength = wordToSearch.length(); for (String line : textArea.getText().split("\\n")) { m = line.indexOf(wordToSearch); if(m == -1) { total += line.length() + 1; continue; } String newLine = line.replaceAll(wordToSearch, wordToReplace); textArea.replaceRange(newLine, total, total + line.length()); total += newLine.length() + 1; } } public static void main(String args[]) { MyTextEditor textEditor = new MyTextEditor(); textEditor.setVisible(true); } 

}