通过 ENTER 键触发 JButtons?

2022-01-24 00:00:00 java swing jframe

我正在为我的数据库开发 GUI 设计.

I'm developing the GUI design for my database.

在设计了大部分框架并对其进行调试后,我需要使用 ENTER 键将选项添加到触发"按钮,而不是仅单击它.

After designing most of the frame and debugging them I released I needed to add the option to 'trigger' buttons using ENTER key, instead of only clicking on it.

我应该怎么做?我目前正在为我的按钮使用 MouseListener 并希望添加一个在按下 ENTER 键时执行相同操作的侦听器.谢谢

How should I go about it? I'm currently using MouseListener for my buttons and want to add a Listener that performs the same actions when ENTER key is pressed. Thank you

推荐答案

使用 Enter 键调用按钮的 Action 是 LAF 问题.这在 Windows 中受支持,但在默认的 Metal LAF 中,您使用空格键.

Using the Enter key to invoke the Action of a button is a LAF issue. This is supported in Windows, but in the default Metal LAF you use the space bar.

查看 输入键和按钮几个解决方案:

  1. 当您从一个按钮切换到另一个按钮时,您可以使用 UIManager 使该按钮成为默认按钮:UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);

您可以使用键绑定来映射 Enter Key 按下/释放事件.

You can use Key Bindings to map the Enter Key pressed/released events.

相关文章