我正在使用Swing HTMLEditorKit生成一个HTML表。其中一列显示了可选择的超链接。
就像在搜索引擎中一样,我想标记已经调用的链接(粗体或着色)。
添加此行为的正确位置在哪里?
编辑:
Gilbert,谢谢你的提示
看起来链接状态并没有得到尊重。第一个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);
} 发布于 2020-07-17 01:44:56
This one给了我解决方案。
稍微修改一下就行了。
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());
}
}https://stackoverflow.com/questions/62877577
复制相似问题