TextField用于单行文本输入。请看下面的示例 -
import javafx.application.Application;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.TextField;import javafx.scene.layout.GridPane;import javafx.stage.Stage;public class Main extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage stage) {Scene scene = new Scene(new Group(), 450, 250);TextField notification = new TextField ();notification.setText("Label");notification.clear();GridPane grid = new GridPane();grid.setVgap(4);grid.setHgap(10);grid.setPadding(new Insets(5, 5, 5, 5));grid.add(new Label("To: "), 0, 0);grid.add(notification, 1, 0);Group root = (Group) scene.getRoot();root.getChildren().add(grid);stage.setScene(scene);stage.show();}}

TextField和Password字段扩展了TextInput类,它是JavaFX中所有文本控件的超类。  
创建文本域
我们可以使用TextField类的构造函数来创建文本字段。
TextField只是一个带有光标的文本输入框,通常我们需要一个Label控件来告诉文本字段的目的。以下代码创建一个Label控件来标记对应的文本字段是用于名称输入。然后它创建一个TextField对象。之后,它使用HBox布局Label和TextField。
Label label1 = new Label("Name:");TextField textField = new TextField ();HBox hb = new HBox();hb.getChildren().addAll(label1, textField);hb.setSpacing(10);

 使用预定义文本创建文本字段。  
TextField textField = new TextField("sss")
TextField文本
要从文本字段获取值,请调用getTexfdt()方法。
从TextInput的setPrefColumnCount方法设置文本字段的大小。 通过设置一次可以显示的最大字符数。
我们可以使用提示字幕通知用户文本字段的用途。setPromptText()方法定义显示在文本字段中的字符串。无法通过getText()方法获取提示文本。
以下代码显示如何设置TextField的提示文本
import javafx.application.Application;import javafx.geometry.Insets;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.TextField;import javafx.scene.layout.GridPane;import javafx.stage.Stage;public class Main extends Application {@Overridepublic void start(Stage stage) {Group root = new Group();Scene scene = new Scene(root, 300, 150);stage.setScene(scene);stage.setTitle("Text Field Sample");GridPane grid = new GridPane();grid.setPadding(new Insets(10, 10, 10, 10));grid.setVgap(5);grid.setHgap(5);scene.setRoot(grid);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);stage.show();}public static void main(String[] args) {launch(args);}}

以下列表有一些有用的方法,可以使用它们在文本字段中进行文本编辑。
- copy() - 将所选文本设置为剪贴板。
 - cut() - 将所选文本设置为剪贴板并删除当前选择。
 - selectAll() - 选择文本输入中的所有文本。
 - paste() - 将剪贴板中的内容设置为此文本并替换当前选择。
 
示例-1
以下代码显示如何将字符串值从TextField绑定到Stage Title。
import javafx.application.Application;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.TextField;import javafx.scene.layout.HBox;import javafx.stage.Stage;public class Main extends Application {StringProperty title = new SimpleStringProperty();public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage stage) {TextField titleTextField;titleTextField = new TextField();titleTextField.setText("Stage Coach");titleTextField.setPrefColumnCount(15);HBox hBox = new HBox();hBox.setSpacing(10);hBox.getChildren().add(new Label("title:"));hBox.getChildren().add(titleTextField);Scene scene = new Scene(hBox,270,270);title.bind(titleTextField.textProperty());stage.setScene(scene);stage.titleProperty().bind(title);stage.show();}}

