我了解如何通过以下方式获取属性:
public void hyperlinkUpdate(HyperlinkEvent e) {
e.getSourceElement().getAttributes().getAttribute(HTML.Attribute.COLOR);
如何更改该属性?
此代码更改元素的样式。希望它会有所帮助...
private void editorHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ENTERED) {
changeStyle(evt.getSourceElement(), "a:hover");
} else if (evt.getEventType() == HyperlinkEvent.EventType.EXITED) {
changeStyle(evt.getSourceElement(), "a");
}
}
private void changeStyle(Element el, String styleName) {
HTMLDocument doc = (HTMLDocument)editor.getDocument();
StyleContext ss = doc.getStyleSheet();
Style style = ss.getStyle(styleName);
int start = el.getStartOffset();
int end = el.getEndOffset();
doc.setCharacterAttributes(start, end - start, style, false);
}