如何在 Swing JEditorPane 中使用击键触发超链接

发布于 2024-08-06 05:03:48 字数 92 浏览 1 评论 0原文

我正在尝试使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-08-13 05:03:48

首先,HyperlinkEvent 仅在不可编辑的 JEditorPane 上触发,因此用户很难知道插入符何时位于链接上。

但如果您确实想这样做,那么您应该使用按键绑定(而不是 KeyListener)将操作绑定到 ENTER KeyStroke。

实现此目的的一种方法是在按下 Enter 键时将 MouseEvent 分派到编辑器窗格来模拟 mouseClick。像这样的东西:

class HyperlinkAction extends TextAction
{
    public HyperlinkAction()
    {
        super("Hyperlink");
    }

    public void actionPerformed(ActionEvent ae)
    {
        JTextComponent component = getFocusedComponent();
        HTMLDocument doc = (HTMLDocument)component.getDocument();
        int position = component.getCaretPosition();
        Element e = doc.getCharacterElement( position );
        AttributeSet as = e.getAttributes();
        AttributeSet anchor = (AttributeSet)as.getAttribute(HTML.Tag.A);

        if (anchor != null)
        {
            try
            {
                Rectangle r = component.modelToView(position);

                MouseEvent me = new MouseEvent(
                    component,
                    MouseEvent.MOUSE_CLICKED,
                    System.currentTimeMillis(),
                    InputEvent.BUTTON1_MASK,
                    r.x,
                    r.y,
                    1,
                    false);

                component.dispatchEvent(me);
            }
            catch(BadLocationException ble) {}
        }
    }
}

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:

class HyperlinkAction extends TextAction
{
    public HyperlinkAction()
    {
        super("Hyperlink");
    }

    public void actionPerformed(ActionEvent ae)
    {
        JTextComponent component = getFocusedComponent();
        HTMLDocument doc = (HTMLDocument)component.getDocument();
        int position = component.getCaretPosition();
        Element e = doc.getCharacterElement( position );
        AttributeSet as = e.getAttributes();
        AttributeSet anchor = (AttributeSet)as.getAttribute(HTML.Tag.A);

        if (anchor != null)
        {
            try
            {
                Rectangle r = component.modelToView(position);

                MouseEvent me = new MouseEvent(
                    component,
                    MouseEvent.MOUSE_CLICKED,
                    System.currentTimeMillis(),
                    InputEvent.BUTTON1_MASK,
                    r.x,
                    r.y,
                    1,
                    false);

                component.dispatchEvent(me);
            }
            catch(BadLocationException ble) {}
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文