突出显示在框架中打开的文本文件的一些单词 [英] Highlighting few of the words of a text file opened in a frame

查看:103
本文介绍了突出显示在框架中打开的文本文件的一些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个框架中打开一个文件,我想强调几个单词。据我所知,我需要遍历文件的内容。如何遍历内容以及我可能用于突出显示的相关属性是什么?

I am opening a file in a frame and I wish to highlight few of the words.As I understand,I need to traverse the content of the file. How do I traverse through the contents and what is the related property that I may use for the purpose of highlighting?

更新:我的代码有点像这样

private JEditorPane editorpane;
JScrollPane editorScrollPane;

public TextEditor()
{
    editorpane = new JEditorPane();
    editorpane.setEditable(false);

    if (filename != null)
    {
        try
        {
            File file = new File(filename);
            editorpane.setPage(file.toURI().toURL());
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.err.println("Attempted to read a bad file ...");
        }
    }
    else
    {
        System.err.println("File name is wrong");
    }

    add(editorpane);
}


推荐答案

要突出显示 JEditorPane 看一下这个例子:

To highlight on a JEditorPane have a look at this example:

import java.awt.Color;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

public class Test {

    // An instance of the subclass of the default highlight painter
    static MyHighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JEditorPane jep = new JEditorPane();
                jep.setText("Hello to the public");
                frame.add(jep);
                frame.pack();
                frame.setVisible(true);

                highlight(jep, "public");//'public is the word to highligh'

            }
        });
    }

    // Creates highlights around all occurrences of pattern in textComp
    public static void highlight(JTextComponent textComp, String pattern) {
        // First remove all old highlights
        removeHighlights(textComp);

        try {
            Highlighter hilite = textComp.getHighlighter();
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;

            // Search for pattern
            while ((pos = text.indexOf(pattern, pos)) >= 0) {
                // Create highlighter using private painter and apply around pattern
                hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
                pos += pattern.length();
            }
        } catch (BadLocationException e) {
        }
    }

    // Removes only our private highlights
    public static void removeHighlights(JTextComponent textComp) {
        Highlighter hilite = textComp.getHighlighter();
        Highlighter.Highlight[] hilites = hilite.getHighlights();

        for (int i = 0; i < hilites.length; i++) {
            if (hilites[i].getPainter() instanceof MyHighlightPainter) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
}

class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

    public MyHighlightPainter(Color color) {
        super(color);
    }
}

这篇关于突出显示在框架中打开的文本文件的一些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆