【问题标题】:Void is an invalid type for the variable highlighttVoid 是变量 highlightt 的无效类型
【发布时间】:2013-11-30 12:58:26
【问题描述】:

所以我在 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());
        }
    });

And On highlightt(field, word.getText()); 未为类型 new ActionListener 定义的错误

谁能帮帮我?谢谢:)。

【问题讨论】:

  • 我怀疑这是上一行的问题,例如未关闭的评论或其他内容。请在文件的前面发布更多代码。
  • 你没有在课堂上声明东西。
  • 在 MyHighlightPainter 的类定义中移动方法 highlightt()。
  • UPDATED WITH EARLIER CODE ??如果您清楚地链接您的问题,而不是希望人们浏览并链接您的旧问题,那会更好。
  • @user2056954 我不得不回滚你上次的编辑。请不要删除您的问题,因为这可能会引起未来的访问者的兴趣。如果您想让我们知道您最终遵循的解决方案,请将其发布为您自己问题的答案。谢谢你:)

标签: java public void


【解决方案1】:

只需将方法定义和实例声明保留在类中 -

这样做

 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

【讨论】:

  • 但随后出现错误:search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { highlightt(field, word.getText()); } });
  • @user2056954 老实说,我在您的问题中没有看到任何此类代码
  • @user2056954 search 变量的声明在哪里?
  • search = new JButton ("Search");你是说这个?
  • 把那部分也移到类里面
猜你喜欢
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多