开火按钮与在JavaFX中输入的OnAction [英] Fire Button's onAction with Enter in JavaFX

查看:2412
本文介绍了开火按钮与在JavaFX中输入的OnAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,JavaFx的。在我的JavaFX应用程序我已经设置OnAction属性,它工作正常,当我preSS使用鼠标的按钮。我想火一样的,即使用户preSS按钮进入。我知道我可以使用,甚至处理程序来做到这一点。
但是,当我读的OnAction的JavaDoc它说,该事件由一个键preSS起火。

I'm a newbie to JavaFx. In my JavaFX application I have set onAction property and it works fine when I press the button using mouse. I want to fire the same even when user press Enter on button. I know I can use a even handler to do that. But when I read the onAction JavaDoc it says that this event get fire by a key press.

属性说明:

按钮的操作,这是无论何时调用
  该按钮被解雇。这可能是由于在用户点击
  按钮,用鼠标,或触摸事件,或由键preSS ,或者
  开发人员编程调用火()方法。

The button's action, which is invoked whenever the button is fired. This may be due to the user clicking on the button with the mouse, or by a touch event, or by a key press, or if the developer programmatically invokes the fire() method.

但是,当我preSS回车键没有任何反应。它是在文档中的错误?还有没有其他的方式来实现,如果没有添加alistener的按钮?

But when I press Enter key nothing happens. Is it error in documentation? Are there any other way to achieve that without adding alistener to the button?

P.S

我用空格键选中的意见然后被解雇后。但我想设置为Enter键。
我有很多的按钮。我试图 button.setDefaultButton(真); 但它不会被解雇。我认为这是becacuse有多个按钮。如果我把它只是一个按钮,它工作正常。如何设置多个按钮?

After the comments I checked with space key then it get fired. But I want to set that to Enter key. I have many buttons. I tried button.setDefaultButton(true); but it is not get fired. I think that is becacuse there are more than one button. If I set it just to a single button it works fine. How to set that to multiple buttons?

推荐答案

您可以动态当前焦点按钮的默认按钮属性通过更改绑定

You can dynamically change the default button property of the currently focused button by using binding

btn.defaultButtonProperty().bind(btn.focusedProperty());

这篇关于开火按钮与在JavaFX中输入的OnAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆