如何选择在JavaFX2中按下的Enter键? [英] How do I pick up the Enter Key being pressed in JavaFX2?

查看:202
本文介绍了如何选择在JavaFX2中按下的Enter键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextField用于输入搜索词,还有一个用于Go的按钮。但是在JavaFX2中,如何按下TextField中的Enter键才会执行操作?

I have a TextField to enter a search term, and a button for "Go". But in JavaFX2, how would I make it so pressing the Enter Key in the TextField would perform an action?

谢谢:)

推荐答案

您可以使用TextField的onAction属性并将其绑定到控制器中的方法。

You can use the onAction attribute of the TextField and bind it to a method in your controller.

@FXML
public void onEnter(ActionEvent ae){
   System.out.println("test") ;
}

在您的FXML文件中:

And in your FXML file:

<TextField fx:id="textfield" layoutX="29.0" layoutY="298.0" onAction="#onEnter" prefWidth="121.0" />

这篇关于如何选择在JavaFX2中按下的Enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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