如何选择在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" />