所以我在Java上遇到了另一个可怕的错误。我有这个代码:
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
public MyHighlightPainter(Color color) {
super(color);
}
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.yellow);
public void highlightt(JTextArea textComp, String pattern) {
try {
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
while((pos=text.toUpperCase().indexOf(pattern.toUpperCase(),pos))>=0) {
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos +=pattern.length();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
highlightt(field, word.getText());
}
});
On highlightt(field, word.getText());
错误,未定义新类型ActionListener
任何人都可以帮助我吗?谢谢:)。
答案 0 :(得分:0)
只需将方法定义和实例声明保留在类 -
中这样做
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
public MyHighlightPainter(Color color) {
super(color);
}
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.yellow);
public void highlightt(JTextArea textComp, String pattern) {
try {
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
while((pos=text.toUpperCase().indexOf(pattern.toUpperCase(),pos))>=0) {
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos +=pattern.length();
}
} catch(Exception e) {
e.printStackTrace();
}
}
} // close class