Java 如何在无例外的情况下在textarea中查找下一个事件?

Java 如何在无例外的情况下在textarea中查找下一个事件?,java,exception,textarea,Java,Exception,Textarea,我一直在做一个项目,通过点击特定单词的按钮来发现下一个事件,但它总是抛出异常 public void highlightnext(JTextComponent textareafield,String pattern) { textareafield.getHighlighter().removeAllHighlights(); //highlighting try { hilit = textarea.getHighlighter();

我一直在做一个项目,通过点击特定单词的按钮来发现下一个事件,但它总是抛出异常

public void highlightnext(JTextComponent textareafield,String pattern) {
    textareafield.getHighlighter().removeAllHighlights();
    //highlighting      
    try { 
        hilit = textarea.getHighlighter();
        painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
        Document doc=textarea.getDocument();
        String area=doc.getText(0, doc.getLength());
        if (pos>area.length())
            return;
        pos=(area.toUpperCase().indexOf(pattern.toUpperCase(),pos));
        pos=(area.toUpperCase().indexOf(pattern.toUpperCase(),pos+1));
        hilit.addHighlight(pos,pos+pattern.length(), painter);
        System.out.println(pos);                    
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
  • 不要每次都将数据转换为大写,否则效率低下。将文本转换为字符串一次,然后在代码中使用这两个字符串

  • 不知道为什么你有两种说法。你只需要一个来做搜索

  • 您需要检查“pos”的值以确保它不是“-1”

  • 下面是一些示例代码。它查找所有出现的文本:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextAndNewLinesTest extends JFrame
    {
        public TextAndNewLinesTest()
            throws Exception
        {
            String text =
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n";
    
            JTextPane textPane = new JTextPane();
            textPane.setText(text);
            JScrollPane scrollPane = new JScrollPane( textPane );
            getContentPane().add( scrollPane );
    
            Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.cyan );
    
    
            String search = "three";
            int offset = 0;
    
            int length = textPane.getDocument().getLength();
            text = textPane.getDocument().getText(0, length);
                //text = textPane.getText();    
    
            while ((offset = text.indexOf(search, offset)) != -1)
            {
                try
                {
                    textPane.getHighlighter().addHighlight(offset, offset + 5, painter); // background
                    offset += search.length();
                }
                catch(BadLocationException ble) {}
            }
        }
    
        public static void main(String[] args)
            throws Exception
        {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new TextAndNewLinesTest();
            frame.setTitle("Text and New Lines - Problem");
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.setSize(400, 120);
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }
    

    如果您一次只想查找一个事件,那么基本上您会将“while”语句更改为“If”语句。

    您会遇到什么样的异常?javax.swing.text.BadLocationException:Invalid start offsetOk。谢谢通常,您应该获得有关错误发生的信息。例如,它阻塞的行。问题在于
    getText
    try to do,只是为了测试,
    doc.getText(0,1)成功了吗?如果
    (pos>area.length())返回,我想就是这个了。尝试`if(pos+pattern.length()>area.length())返回`
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextAndNewLinesTest extends JFrame
    {
        public TextAndNewLinesTest()
            throws Exception
        {
            String text =
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n" +
                "one two three four five\r\n";
    
            JTextPane textPane = new JTextPane();
            textPane.setText(text);
            JScrollPane scrollPane = new JScrollPane( textPane );
            getContentPane().add( scrollPane );
    
            Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter( Color.cyan );
    
    
            String search = "three";
            int offset = 0;
    
            int length = textPane.getDocument().getLength();
            text = textPane.getDocument().getText(0, length);
                //text = textPane.getText();    
    
            while ((offset = text.indexOf(search, offset)) != -1)
            {
                try
                {
                    textPane.getHighlighter().addHighlight(offset, offset + 5, painter); // background
                    offset += search.length();
                }
                catch(BadLocationException ble) {}
            }
        }
    
        public static void main(String[] args)
            throws Exception
        {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new TextAndNewLinesTest();
            frame.setTitle("Text and New Lines - Problem");
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.setSize(400, 120);
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    }