所以我想知道如何在java文本窗格中主动更改关键字的颜色。我理解必须使用文档监听器,但目前它似乎没有工作,实际上把它放在文件监听器中导致我无法正常打开文件或颜色。那么我怎样才能主动调用一个改变java中关键字颜色的方法。这是搜索关键字的代码,它在我打开文件时起作用,而不是主动。
public void findKeyWords(String directory) throws FileNotFoundException
{
final StyleContext cont = StyleContext.getDefaultStyleContext();
final AttributeSet jKeyWord = cont.addAttribute(cont.getEmptySet(),
StyleConstants.Foreground,Color.RED);
final AttributeSet jOperator = cont.addAttribute(cont.getEmptySet(),
StyleConstants.Foreground,Color.MAGENTA);
final AttributeSet jtypes = cont.addAttribute(cont.getEmptySet(),
StyleConstants.Foreground,Color.CYAN);
ArrayList<String> words = loadKeyWords(directory);
for (String line : words)
{
searchJava(line,jKeyWord);
}
ArrayList<String> operators = loadOperators(directory);
for (String line : operators)
{
searchJava(line, jOperator);
}
ArrayList<String> types1 = loadTypes(directory);
for (String line : types1)
{
searchJava(line, jtypes);
}
}
private ArrayList<String> loadKeyWords(String directory) throws FileNotFoundException
{
ArrayList<String> javaWords = new ArrayList<String>();
final String dir = System.getProperty("user.dir");
File file = new File(dir + "/" + directory + "/keywords.txt");
Scanner scan = new Scanner(file);
while(scan.hasNext())
{
javaWords.add(scan.next() + " ");
}
scan.close();
return javaWords;
}
private ArrayList<String> loadOperators(String directory) throws FileNotFoundException
{
ArrayList<String> javaWords = new ArrayList<String>();
final String dir = System.getProperty("user.dir");
File file = new File(dir + "/" + directory + "/operators.txt");
Scanner scan = new Scanner(file);
while(scan.hasNext())
{
javaWords.add(scan.next());
}
scan.close();
return javaWords;
}
private ArrayList<String> loadTypes(String directory) throws FileNotFoundException
{
ArrayList<String> javaWords = new ArrayList<String>();
final String dir = System.getProperty("user.dir");
File file = new File(dir + "/" + directory + "/types.txt");
Scanner scan = new Scanner(file);
while(scan.hasNext())
{
javaWords.add(" " + scan.next());
}
scan.close();
return javaWords;
}
public void searchJava(String wordToSearch, AttributeSet javaAttr)
{
final AttributeSet attr = javaAttr;
Document text = textArea.getDocument();
int m;
int t;
int total = 0;
for (String line : textArea.getText().split("\n"))
{
m = line.indexOf(wordToSearch);
if(m == -1)
{
if(isUnix())
{
total += line.length() + 1;
}
else if(isWindows())
{
total += line.length();
}
else if(isMac())
{
total += line.length() + 1;
}
else
{
total += line.length() + 1;
}
continue;
}
try{
text.remove(total + m, wordToSearch.length());
text.insertString(total + m, wordToSearch, attr);
}catch(BadLocationException ex)
{}
while(true)
{
m = line.indexOf(wordToSearch, m + 1 );
if (m == -1)
{
break;
}
try
{
text.remove(total + m, wordToSearch.length());
text.insertString(total + m, wordToSearch, attr);
}catch(BadLocationException e)
{
}
}
if(isUnix())
{
total += line.length() + 1;
}
else if(isWindows())
{
total += line.length();
}
else if(isMac())
{
total += line.length() + 1;
}
else
{
JOptionPane.showMessageDialog(null, "Eric You Troll" );
total += line.length() + 1;
}
}
}