通过ENTER键触发JButtons?

时间:2014-05-20 20:34:07

标签: java swing jframe

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

在设计了大部分框架并对其进行调试后,我发布了我需要添加选项来触发'按钮使用 ENTER 键,而不是仅点击它。

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

2 个答案:

答案 0 :(得分:2)

您应该使用任何类型的按钮MouseListerner,而应该使用ActionListener。当键盘根据外观要求更多激活按钮时,这将提醒您

请查看How to Write an Action ListenersHow to Use Buttons, Check Boxes, and Radio Buttons了解详情

答案 1 :(得分:2)

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

查看Enter Key and Button了解几个解决方案:

  1. 您可以使用UIManager将按钮设置为默认按钮,从按钮到按钮选项卡:UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);

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

相关问题