Java 如何更改JEditorPane中HTMLDocument的特定元素的颜色?

Java 如何更改JEditorPane中HTMLDocument的特定元素的颜色?,java,jeditorpane,dom,Java,Jeditorpane,Dom,我基本上想实现在我将鼠标悬停在链接上时更改链接的颜色。当我将鼠标移到链接上时触发的HyperlinkEvent会将HTML元素交给我,但它不允许我在其上设置任何样式属性,而且我也不知道如何获取具有可设置属性的元素 见 JTextArea textComp=newjtextarea(); //突出显示“公共”一词的出现 突出显示(textComp,“公共”); //在textComp中所有出现的图案周围创建高光 公共空白突出显示(JTextComponent textComp,字符串模式){ //

我基本上想实现在我将鼠标悬停在链接上时更改链接的颜色。当我将鼠标移到链接上时触发的HyperlinkEvent会将HTML元素交给我,但它不允许我在其上设置任何样式属性,而且我也不知道如何获取具有可设置属性的元素

JTextArea textComp=newjtextarea();
//突出显示“公共”一词的出现
突出显示(textComp,“公共”);
//在textComp中所有出现的图案周围创建高光
公共空白突出显示(JTextComponent textComp,字符串模式){
//首先删除所有旧的高光
移除突出显示(textComp);
试一试{
Hilliter hilite=textComp.getHighlighter();
Document doc=textComp.getDocument();
String text=doc.getText(0,doc.getLength());
int pos=0;
//搜索模式
而((pos=text.indexOf(pattern,pos))>=0){
//使用私人画师创建荧光笔并在图案周围应用
hilite.addHighlight(pos,pos+pattern.length(),myHighlightPainter);
pos+=pattern.length();
}
}捕获(错误位置异常e){
}
}
//只删除我们的私人高光
公共void removeHighlights(JTextComponent textComp){
Hilliter hilite=textComp.getHighlighter();
hilliter.Highlight[]hilites=hilite.getHighlights();

对于(int i=0;i,我通过使用样式化文档和来自HTMLEditorKit的一些帮助,找到了我想要做的事情:

public class HighlightHyperlinkExample {
    private static Element lastHyperlinkElementEntered;
    private static JEditorPane textPane;


    public static void main(String[] args) {
        textPane = new JEditorPane();
        textPane.setContentType(new HTMLEditorKit().getContentType());
        JScrollPane scrollPane = new JScrollPane(textPane);
        textPane.setText(
                "Sample text with <a href=\"x\">a link</a> and another <a href=\"x\">link</a>.");

        initListeners();

        JFrame frame = new JFrame();
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }


    private static void initListeners() {
        textPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                removeHyperlinkHighlight();
            }
        });
        textPane.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                Point pt = new Point(e.getX(), e.getY());
                int pos = textPane.viewToModel(pt);
                if (pos >= 0) {
                    HTMLDocument hdoc = (HTMLDocument) textPane.getDocument();
                    Element elem = hdoc.getCharacterElement(pos);
                    if (elem != null) {
                        AttributeSet a = elem.getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(elem);
                        } else {
                            removeHyperlinkHighlight();
                        }
                    }
                }
            }
        });
    }

    private static void removeHyperlinkHighlight() {
        changeColor(lastHyperlinkElementEntered, Color.BLUE);
        lastHyperlinkElementEntered = null;
    }

    private static void highlightHyperlink(Element hyperlinkElement) {
        if (hyperlinkElement != lastHyperlinkElementEntered) {
            lastHyperlinkElementEntered = hyperlinkElement;
            changeColor(hyperlinkElement, Color.RED);
        }
    }

    private static void changeColor(Element el, Color color) {
        if (lastHyperlinkElementEntered != null) {
            HTMLDocument doc = (HTMLDocument) textPane.getDocument();
            int start = el.getStartOffset();
            int end = el.getEndOffset();
            StyleContext ss = doc.getStyleSheet();
            Style style = ss.addStyle("HighlightedHyperlink", null);
            style.addAttribute(StyleConstants.Foreground, color);
            doc.setCharacterAttributes(start, end - start, style, false);
        }
    }
}
公共类HighlightHyperlinkExample{
私有静态元素lastHyperlinkelementered;
私有静态窗格文本窗格;
公共静态void main(字符串[]args){
textPane=new-JEditorPane();
textPane.setContentType(新的HTMLEditorKit().getContentType());
JScrollPane scrollPane=新的JScrollPane(textPane);
textPane.setText(
“带和另一个的示例文本。”);
initListeners();
JFrame=新JFrame();
frame.add(滚动窗格);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
私有静态void initListeners(){
textPane.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseExited(MouseEvent e){
removeHyperlinkHighlight();
}
});
textPane.addMouseMotionListener(新的MouseMotionListener(){
公共无效鼠标标记(鼠标事件e){
}
public void mouseMoved(MouseEvent e){
点pt=新点(e.getX(),e.getY());
int pos=textPane.viewToModel(pt);
如果(位置>=0){
HTMLDocument hdoc=(HTMLDocument)textPane.getDocument();
Element elem=hdoc.getCharacterElement(位置);
if(elem!=null){
AttributeSet a=elem.getAttributes();
AttributeSet anchor=(AttributeSet)a.getAttribute(HTML.Tag.a);
if(锚点!=null){
//仅高亮显示锚定标记
highlightHyperlink(elem);
}否则{
removeHyperlinkHighlight();
}
}
}
}
});
}
私有静态void removeHyperlinkHighlight(){
changeColor(LastHyperlinkelementered,Color.BLUE);
lastHyperlinkElementEntered=null;
}
私有静态void highlightHyperlink(元素hyperlinkElement){
if(hyperlinkElement!=lastHyperlinkElementEntered){
lastHyperlinkElementEntered=hyperlinkElement;
changeColor(hyperlinkElement,Color.RED);
}
}
私有静态void changeColor(元素el,颜色){
if(lastHyperlinkelementered!=null){
HTMLDocument doc=(HTMLDocument)textPane.getDocument();
int start=el.getStartOffset();
int end=el.getEndOffset();
StyleContext ss=doc.getStyleSheet();
Style Style=ss.addStyle(“HighlightedHyperlink”,null);
style.addAttribute(StyleConstants.Foreground,color);
doc.setCharacterAttributes(开始、结束-开始、样式、false);
}
}
}

谢谢。我完全忘记了Highlighter。不幸的是,我想做的是改变前景,而不是背景,这对于Highlighter来说要困难得多。但是,它确实让我走上了正确的道路。所以,谢谢。我只是想在打开你的链接[在JTextComponent中突出显示单词]时说这句话它链接到一个带有病毒的页面。考虑在AythHythTythPrLink中重写实际的颜色,并使用在AdvielHyviLkkHyLyt中记住的颜色。
public class HighlightHyperlinkExample {
    private static Element lastHyperlinkElementEntered;
    private static JEditorPane textPane;


    public static void main(String[] args) {
        textPane = new JEditorPane();
        textPane.setContentType(new HTMLEditorKit().getContentType());
        JScrollPane scrollPane = new JScrollPane(textPane);
        textPane.setText(
                "Sample text with <a href=\"x\">a link</a> and another <a href=\"x\">link</a>.");

        initListeners();

        JFrame frame = new JFrame();
        frame.add(scrollPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }


    private static void initListeners() {
        textPane.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                removeHyperlinkHighlight();
            }
        });
        textPane.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                Point pt = new Point(e.getX(), e.getY());
                int pos = textPane.viewToModel(pt);
                if (pos >= 0) {
                    HTMLDocument hdoc = (HTMLDocument) textPane.getDocument();
                    Element elem = hdoc.getCharacterElement(pos);
                    if (elem != null) {
                        AttributeSet a = elem.getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(elem);
                        } else {
                            removeHyperlinkHighlight();
                        }
                    }
                }
            }
        });
    }

    private static void removeHyperlinkHighlight() {
        changeColor(lastHyperlinkElementEntered, Color.BLUE);
        lastHyperlinkElementEntered = null;
    }

    private static void highlightHyperlink(Element hyperlinkElement) {
        if (hyperlinkElement != lastHyperlinkElementEntered) {
            lastHyperlinkElementEntered = hyperlinkElement;
            changeColor(hyperlinkElement, Color.RED);
        }
    }

    private static void changeColor(Element el, Color color) {
        if (lastHyperlinkElementEntered != null) {
            HTMLDocument doc = (HTMLDocument) textPane.getDocument();
            int start = el.getStartOffset();
            int end = el.getEndOffset();
            StyleContext ss = doc.getStyleSheet();
            Style style = ss.addStyle("HighlightedHyperlink", null);
            style.addAttribute(StyleConstants.Foreground, color);
            doc.setCharacterAttributes(start, end - start, style, false);
        }
    }
}