如何在 Swing JEditorPane 中使用击键触发超链接
我正在尝试使用“Enter”键触发 JEditorPane 中的超链接。这样插入符号下的超链接(如果有)就会触发,而不必用鼠标单击。
任何帮助将不胜感激。
I'm trying to fire hyperlinks in a JEditorPane using the "Enter" keystroke. So that the hyperlink (if any) under the caret will fire rather than having to click with the mouse.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,HyperlinkEvent 仅在不可编辑的 JEditorPane 上触发,因此用户很难知道插入符何时位于链接上。
但如果您确实想这样做,那么您应该使用按键绑定(而不是 KeyListener)将操作绑定到 ENTER KeyStroke。
实现此目的的一种方法是在按下 Enter 键时将 MouseEvent 分派到编辑器窗格来模拟 mouseClick。像这样的东西:
First of all the HyperlinkEvent is only fired on a non-editable JEditorPane so it will be difficult for the users to know when the caret is over a link.
But if you do want to do this, then you should be using Key Bindings (not a KeyListener) to bind an Action to the ENTER KeyStroke.
One way to do this is to simulate a mouseClick by dispatching a MouseEvent to the editor pane when the Enter key is pressed. Something like this: