如何使用 JavaFX 创建密码字段?
文本字段接受并显示文本。在最新版的 JavaFX 中,它只接受单行文本。在 JavaFX 中,javafx.scene.control.TextField 类表示文本字段,此类继承javafx.scene.control.TextInputControl(所有文本控件的基类)类。使用它可以从用户接收输入并在应用程序中读取。
与文本字段类似,密码字段接受文本,但并不显示输入文本,而是通过显示回显字符串隐藏输入的字符。
在 JavaFX 中,javafx.scene.control.PasswordField 表示密码字段,它继承自 Text 类。要创建密码字段,你需要实例化此类。
示例
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PasswordFieldExample extends Application {
public void start(Stage stage) {
//Creating nodes
TextField textField = new TextField();
PasswordField pwdField = new PasswordField();
//Creating labels
Label label1 = new Label("Name: ");
Label label2 = new Label("Pass word: ");
//Adding labels for nodes
HBox box = new HBox(5);
box.setPadding(new Insets(25, 5 , 5, 50));
box.getChildren().addAll(label1, textField, label2, pwdField);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Password Field Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

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