如何在 JPopupMenu 中获得焦点

发布于 2024-12-21 00:13:19 字数 309 浏览 4 评论 0原文

我是一名经验丰富的 Java 程序员,但还是一名 Swing 新手,所以请耐心等待。

我希望有一个具有键盘焦点的 JPopupWindow 。我想响应箭头键、Escape(关闭菜单)和 Enter(调用该项目)。我是否必须在菜单中添加一个 KeyListener 并自己编写所有这些代码,或者是否可以设置某种“模式”来激活此行为,这似乎应该是标准的。

JPopupMenus 上的标准 Swing 教程部分仅涉及助记符和加速器。我不是特别想要那些。

很难相信这完全违背了 JPopupMenu 的原则。

实现我想要的东西的最佳方法是什么?

I am an experienced Java programmer, but a Swing newbie so please bear with me.

I wish to have a JPopupWindow which has keyboard focus. I want to respond to arrow keys, escape (to close the menu) and Enter (to invoke the item). Must I add a KeyListener to the menu and code all this myself, or is there some sort of "mode" I can set to activate this behavior which seems like it ought to be standard.

The standard Swing tutorial section on JPopupMenus speaks only of mnemonics and accelerators. I don't particularly want those.

Hard to believe that this is totally against the grain of JPopupMenu.

What is best way to get something like I want implemented?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

说好的呢 2024-12-28 00:13:19

一旦你执行jpopupMenu.show(Component invoker, int x, int y),它就会根据你的需要自动响应键盘。如果这不起作用,那么您可能需要解决其他一些问题,即其他一些组件在不应该夺走菜单焦点的情况下夺走了焦点等。

Once you do jpopupMenu.show(Component invoker, int x, int y)it will automatically respond to keyboard as you want. If this is not working then you might need to fix some other issue i.e some other component is snatching away focus from the menu when it should not etc.

滥情空心 2024-12-28 00:13:19

我建议添加 < 而不是 KeyListener em>Action 实例到您的 JPopupMenu。使用 setComponentPopupMenu() 将弹出窗口添加到面板中。

Instead of KeyListener, I'd recommend adding Action instances to your JPopupMenu. Use setComponentPopupMenu() to add the popup to your panel.

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