下面列出了javax.swing.JTextPane#setCursor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void mouseMoved(MouseEvent e) {
JTextPane pane = (JTextPane)e.getSource();
StyledDocument doc = pane.getStyledDocument();
Element elem = doc.getCharacterElement(pane.viewToModel(e.getPoint()));
AttributeSet as = elem.getAttributes();
if (StyleConstants.isUnderline(as)) {
pane.setCursor(new Cursor(Cursor.HAND_CURSOR));
} else {
pane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
public void mouseMoved(MouseEvent ev) {
JTextPane editor = (JTextPane) ev.getSource();
editor.setEditable(false);
Point pt = new Point(ev.getX(), ev.getY());
int pos = editor.viewToModel(pt);
if (pos >= 0) {
Document eDoc = editor.getDocument();
if (eDoc instanceof DefaultStyledDocument) {
DefaultStyledDocument hdoc =
(DefaultStyledDocument) eDoc;
Element e = hdoc.getCharacterElement(pos);
AttributeSet a = e.getAttributes();
AttributeSet tagA = (AttributeSet) a.getAttribute(HTML.Tag.A);
String href = null;
if (tagA!=null){
href = (String)tagA.getAttribute(HTML.Attribute.HREF);
}
if (href != null) {
editor.setToolTipText(href);
if (editor.getCursor().getType() != Cursor.HAND_CURSOR) {
editor.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
else {
editor.setToolTipText(null);
if (editor.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
editor.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
}
}
else {
editor.setToolTipText(null);
}
}
@Override
protected JComponent getTextComponent(String locator) throws Exception {
JTextPane textComponent = new JTextPane();
textComponent.setText(getContent(locator));
textComponent.setFont(new Font("monospaced", Font.PLAIN, 12));
textComponent.setEditable(false);
textComponent.setCaretPosition(0);
textComponent.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
return textComponent;
}
protected JComponent getTextComponent(String locator) throws Exception {
JTextPane textComponent = new JTextPane();
textComponent.setText(getContent(locator));
textComponent.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
textComponent.setEditable(false);
textComponent.setCaretPosition(0);
return textComponent;
}