TextField(文本框)

JavaFX TextField 控件使 JavaFX 应用程序的用户能够输入文本,然后应用程序可以读取这些文本。JavaFX TextField 控件由 javafx.scene.control.TextField 类表示。

A JavaFX TextField control enables users of a JavaFX application to enter text which can then be read by the application. The JavaFX TextField control is represented by the class javafx.scene.control.TextField.

创建 TextField

您可以通过创建 TextField 类的实例来创建 TextField 控件。这是一个 JavaFX TextField 实例化的示例:

You create a TextField control by creating an instance of the TextField class. Here is a JavaFX TextField instantiation example:

  1. TextField textField = new TextField();

TextField 添加到场景图中

要使 JavaFX TextField 可见,必须将 TextField 对象添加到场景图中。这意味着将其添加到 Scene 对象,或作为附加到 Scene 对象的布局组件的子节点。

For a JavaFX TextField to be visible the TextField object must be added to the scene graph. This means adding it to a Scene object, or as child of a layout which is attached to a Scene object.

这是一个将 JavaFX TextField 附加到场景图的示例:

Here is an example that attaches a JavaFX TextField to the scene graph:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.TextField;
  5. import javafx.scene.layout.HBox;
  6. import javafx.stage.Stage;
  7. public class TextFieldExperiments extends Application {
  8. @Override
  9. public void start(Stage primaryStage) throws Exception {
  10. primaryStage.setTitle("HBox Experiment 1");
  11. TextField textField = new TextField();
  12. HBox hbox = new HBox(textField);
  13. Scene scene = new Scene(hbox, 200, 100);
  14. primaryStage.setScene(scene);
  15. primaryStage.show();
  16. }
  17. public static void main(String[] args) {
  18. Application.launch(args);
  19. }
  20. }

运行上述 JavaFX TextField 示例的结果如下所示:

The result of running the above JavaFX TextField example is an application that looks like this:

A JavaFX TextField component displayed in the scene graph.

获取 TextField 中的文本

你可以使用它的 getText() 方法获取输入到 TextField 中的文本,该方法返回一个字符串类型。这是一个完整的示例,它显示了一个 TextField 和一个 Button,并在单击按钮时读取输入到 TextField 中的文本:

You can get the text entered into a TextField using its getText() method which returns a String. Here is a full example that shows a TextField and a Button and which reads the text entered into the TextField when the button is clicked:

  1. package com.jenkov.javafx.controls;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.control.TextField;
  6. import javafx.scene.layout.HBox;
  7. import javafx.stage.Stage;
  8. public class TextFieldExperiments extends Application {
  9. @Override
  10. public void start(Stage primaryStage) throws Exception {
  11. primaryStage.setTitle("HBox Experiment 1");
  12. TextField textField = new TextField();
  13. Button button = new Button("Click to get text");
  14. button.setOnAction(action -> {
  15. System.out.println(textField.getText());
  16. });
  17. HBox hbox = new HBox(textField, button);
  18. Scene scene = new Scene(hbox, 200, 100);
  19. primaryStage.setScene(scene);
  20. primaryStage.show();
  21. }
  22. public static void main(String[] args) {
  23. Application.launch(args);
  24. }
  25. }

设置 TextField 的文本

您可以使用 setText() 方法设置 TextField 的文本。当您需要为作为表单一部分的文本框设置初始值时,这通常很有用。例如,编辑现有对象或记录。下面是一个设置 JavaFX TextField 文本的简单示例:

You can set the text of a TextField using its setText() method. This is often useful when you need to set the initial value for a text field that is part of a form. For instance, editing an existing object or record. Here is a simple example of setting the text of a JavaFX TextField:

  1. textField.setText("Initial value");