JavaFX UI控件教程(九)之Text Field
翻译自
本章讨论文本字段控件的功能。
的TextField类实现接受并显示文本输入的UI控制。它提供了从用户接收文本输入的功能。与另一个文本输入控件一起,PasswordField此类扩展了TextInput类,它是通过JavaFX API提供的所有文本控件的超类。
显示了带有标签的典型文本字段。
图8-1标签和文本字段
创建文本字段
在,文本字段与标签结合使用,以指示应在字段中键入的内容的类型。
示例8-1创建文本字段
Label label1 = new Label("Name:"); TextField textField = new TextField (); HBox hb = new HBox(); hb.getChildren().addAll(label1, textField); hb.setSpacing(10);您可以创建一个空文本字段,如所示,或者创建一个包含特定文本数据的文本字段。要使用预定义文本创建文本字段,请使用以下TextField类的构造函数:TextField("Hello World!")。您可以通过调用getText方法随时获取文本字段的值。
您可以应用类的setPrefColumnCount方法TextInput来设置文本字段的大小,定义为一次可以显示的最大字符数。
使用文本字段构建UI
通常,TextField对象在表单中用于创建多个文本字段。的应用程序显示三个文本字段,并处理用户在其中输入的数据。
图8-2 TextFieldSample应用程序

的代码片段创建了三个文本字段和两个按钮,并使用GridPane容器将它们添加到应用程序的场景中。当您需要为UI控件实现灵活的布局时,此容器特别方便。
示例8-2向应用程序添加文本字段
//Creating a GridPane container GridPane grid = new GridPane(); grid.setPadding(new Insets(10, 10, 10, 10)); grid.setVgap(5); grid.setHgap(5); //Defining the Name text field final TextField name = new TextField(); name.setPromptText("Enter your first name."); name.setPrefColumnCount(10); name.getText(); GridPane.setConstraints(name, 0, 0); grid.getChildren().add(name); //Defining the Last Name text field final TextField lastName = new TextField(); lastName.setPromptText("Enter your last name."); GridPane.setConstraints(lastName, 0, 1); grid.getChildren().add(lastName); //Defining the Comment text field final TextField comment = new TextField(); comment.setPrefColumnCount(15); comment.setPromptText("Enter your comment."); GridPane.setConstraints(comment, 0, 2); grid.getChildren().add(comment); //Defining the Submit button Button submit = new Button("Submit"); GridPane.setConstraints(submit, 1, 0); grid.getChildren().add(submit); //Defining the Clear button Button clear = new Button("Clear"); GridPane.setConstraints(clear, 1, 1); grid.getChildren().add(clear);花点时间研究代码片段。的name,lastName和comment文本字段使用的空构造函数来创建TextField类。与不同,此代码片段中的文本字段不附带标签。相反,提示字幕会通知用户在文本字段中输入的数据类型。该setPromptText方法定义应用程序启动时出现在文本字段中的字符串。当添加到应用程序时,它会产生所示的输出。
图8-3包含提示消息的三个文本字段

提示文本与文本字段中输入的文本之间的区别在于无法通过该getText方法获取提示文本。
在实际应用程序中,输入到文本字段的数据将根据特定业务任务所需的应用程序逻辑进行处理。下一节将介绍如何使用文本字段评估输入的数据并生成对用户的响应。
处理文本字段数据
如前所述,用户输入文本字段的文本数据可以通过类的getText方法获得TextInput。
学习,学习如何处理TextField对象的数据。
示例8-3定义提交和清除按钮的操作
//Adding a Label final Label label = new Label(); GridPane.setConstraints(label, 0, 3); GridPane.setColumnSpan(label, 2); grid.getChildren().add(label); //Setting an action for the Submit button submit.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { if ((comment.getText() != null && !comment.getText().isEmpty())) { label.setText(name.getText() + " " + lastName.getText() + ", " + "thank you for your comment!"); } else { label.setText("You have not left a comment."); } } }); //Setting an action for the Clear button clear.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { name.clear(); lastName.clear(); comment.clear(); label.setText(null); } });Label添加到GridPane容器的控件呈现应用程序对用户的响应。当用户单击“提交”按钮时,该setOnAction方法将检查comment文本字段。如果它包含非空字符串,则会呈现感谢信息。否则,应用程序通知用户尚未留下注释消息,所示。
图8-4注释文本字段留空

当用户单击“清除”按钮时,将在所有三个文本字段中删除内容。
查看一些可用于文本字段的有用方法。
copy()- 将文本中当前选定的范围传输到剪贴板,保留当前选择。
cut()- 将文本中当前选定的范围传输到剪贴板,删除当前选择。
paste()- 将剪贴板中的内容传输到此文本中,替换当前选择。
相关的API文档