How to highlight keywords in java while opening a

2020-03-31 05:53发布

So I am trying to highlight the keywords in java,which I have stored in a text file, as the user opens a .java file or writes to .java file. I think I know how to tell if the file is of the right type. However, I do not know how to change the color of certain keywords. If anyone could help out that would be great because right now it's pretty confusing. I was wondering if I could use my replace function somehow. I have tried to go about trying to do this with the few methods I have, yet it's still not clear. I have taken out he majority of the methods and listeners, just know they are there but kept out to make it easier to read.

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<String> wordsInTA = new ArrayList<String>();
  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<String> loadJavaWords() throws FileNotFoundException
{
  ArrayList<String> javaWords = new ArrayList<String>();
  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<String> javaWords = loadJavaWords();
  int min = 0;
  int max = javaWords.size()-1;
  while(min <= max)
  {
     int index = (max + min)/2;
     String guess = javaWords.get(index);
     int result = word.compareTo(guess);
     if(result == 0)
     {
        return true;
     }
     else if(result > 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);
}

}

0条回答
登录 后发表回答