有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在窗格中突出显示一个单词

我必须突出显示^{中出现的所有单词。为此,我使用以下代码:

 try
{          
javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter = 
    new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
textPane.getHighlighter().addHighlight(startPos, endPos, 
highlightPainter);
}
catch(Exception ex)
{
}

但是我怎样才能给出一个词的索引位置呢

我正在从文件中读取内容,但它也在读取HTML标记,这干扰了单词索引


共 (3) 个答案

  1. # 1 楼答案

    您可以执行以下操作:

    getHighlighter().addHighlight(start, end, 
             new DefaultHighlighter.DefaultHighlightPainter(Color.red));
    
  2. # 2 楼答案

    基本上,你应该能够在文档中找到你需要的匹配项

    enter image description here

    public class TestEditorPane01 {
    
        public static void main(String[] args) {
            new TestEditorPane01();
        }
    
        public TestEditorPane01() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JEditorPane editor = new JEditorPane();
                    try {
                        editor.setPage(new File("Test.html").toURI().toURL());
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(editor));
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    Document document = editor.getDocument();
                    try {
                        String find = "Method";
                        for (int index = 0; index + find.length() < document.getLength(); index++) {
                            String match = document.getText(index, find.length());
                            if (find.equals(match)) {
                                javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter =
                                        new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                                editor.getHighlighter().addHighlight(index, index + find.length(),
                                        highlightPainter);
                            }
                        }
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
    
                }
            });
        }
    }
    

    这将遍历整个文档并突出显示所有匹配项。这也是一个案例敏感匹配;)

  3. # 3 楼答案

    以下是我的案例,需要在EditorPane中突出显示一个搜索词:

        // text in EditPane
        String text = rSyntaxTextArea.getText();
        if (text != null && !"".equals(filterText.getText())) {
            Highlighter hilit = new RSyntaxTextAreaHighlighter();
            rSyntaxTextArea.setHighlighter(hilit);  
            for (int index = text.toUpperCase().indexOf(
                    // searched text
                    filterText.getText().toUpperCase()); index >= 0; index = text
                    .toUpperCase().indexOf(
                            filterText.getText().toUpperCase(), index + 1)) {
                int end = index + filterText.getText().length();
                HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(
                        Color.LIGHT_GRAY);
                try {
                    hilit.addHighlight(index, end, painter);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
    

    希望有帮助