格式化JEditorPane的光标、工具提示和链接。

3

现在,我正在使用Java Swing创建JEditorPane,主要是因为它具有超链接的能力。我已经成功地显示了链接,并使它们在单击时执行相应的操作,但我遇到了一些格式上的问题。

  1. How can I set the cursor so that it normally is an arrow, but changes to a text cursor when hovering over text? (In essence, the behavior a cursor has within a web browser). I tried

    EditorPane.setCursor(new Cursor(Cursor.TEXT_CURSOR))
    

    but that made it a text cursor everywhere, even when not hovering over text. Right now, hovering over a link shows a pointer hand; I'd like to maintain that functionality as well.

  2. What is the best way to show tooltips or mouseover text when hovering over a link? I tried modifying the title attribute of the link but nothing showed up.

  3. I was trying to implement links to skip down to a subsection of the page, much like http://en.wikipedia.org/wiki/Xkcd#History would take you directly to the History subsection of Wikipedia's xkcd page. How can I do this?

任何一个问题的回答都可以,请尽情提问(如果有多个问题更好)。非常感谢您的帮助!
3个回答

3

正如您所说,一个人也可以只回答一个问题,让我逐一尝试,这里是您上一个第三点的答案:

只需像这样为您的标签提供一个id:

<h1><a id = "top"></a>First Line</h1>

现在,在您页面的底部写入以下内容:
<p><a href = "#top">Return to TOP</a></p>

点击此链接,您将跳转到上述页面区域。

2

可以使用这里提到的方法来解决第1和第2点。特别是,视图/模型转换方法将允许您分别设置setCursor()getToolTipText()的条件。


2
你可以从这里获取源代码http://java-sl.com/JEditorPaneStructureTool.html。它展示了如何获取文本视图边界。首先,你需要使用viewToModel()方法获取当前鼠标指针的插入符位置。然后,沿着Views树向下找到叶子视图并计算其边界。请参阅此http://java-sl.com/tip_view_rectangle.html
如果你的鼠标指针在视图的矩形内,则表示你的鼠标悬停在文本上。
你可以检查插入符位置的文本是否是链接,并显示你的工具提示。
使用这个http://java-sl.com/tip_links_in_editable.html来查看如何检测鼠标是否悬停在链接上。
第三点由@nIcE cOw回答。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接