0

我正在使用 Swing 生成一个 HTML 表格HTMLEditorKit。一栏显示可选择的超链接。

就像在搜索引擎中一样,我想标记已经调用的链接(粗体或着色)。

添加此行为的正确位置在哪里?

编辑:

吉尔伯特,谢谢你的提示

似乎不尊重链接状态。第一个 addRule 行不改变颜色,但保持默认的蓝色字体。第二个注释掉的行作品。

    ...
    HTMLEditorKit kit = new HTMLEditorKit();
    StyleSheet css = kit.getStyleSheet();
    if (css.getStyleSheets() == null) {
        StyleSheet css2 = new StyleSheet();
        css2.addRule("a:link {color: #DDDDDD } a:visited {color: #DDDDDD } a:hover {color: #DDDDDD } a:active {color: #DDDDDD } "); 
//          css2.addRule("a {color: #DDDDDD }"); 
        css2.addStyleSheet(css);
        kit.setStyleSheet(css2);
    }       
4

1 回答 1

0

这个给了我解决方案。

稍作修改对我有用

    jEditorPane.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                HyperlinkEvent.EventType type = e.getEventType();
                
                if (type == HyperlinkEvent.EventType.ACTIVATED) {
                    
                    // mark activated
                    if (e.getSourceElement() != null) {
                        AttributeSet a = e.getSourceElement().getAttributes();
                        AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
                        if (anchor != null) {
                            //only highlight anchor tags
                            highlightHyperlink(e.getSourceElement());
                        }
                    }
于 2020-07-16T17:44:55.740 回答