如何使用 JavaFX 创建文本字段?
文本字段用于接受和显示文本。在最新版本的 JavaFX 中,它只接受单行输入。在 JavaFX 中,**javafx.scene.control.TextField** 类表示文本字段,此类继承 TextInputControl(所有文本控件的基类)类。使用它,你可以接受用户的输入,并将其读取到应用程序中。
要创建一个文本字段,你需要实例化**TextField** 类,还可以将字符串值传递给此类的构造函数,该值将作为文本字段的初始文本。
例如
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextFieldExample extends Application {
public void start(Stage stage) {
//Creating nodes
TextField textField1 = new TextField("Enter your name");
TextField textField2 = new TextField("Enter your e-mail");
//Creating labels
Label label1 = new Label("Name: ");
Label label2 = new Label("Email: ");
//Adding labels for nodes
HBox box = new HBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(label1, textField1, label2, textField2);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Text Field Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP