如何驗證 JavaFX 密碼欄位中輸入的密碼?
文字欄位接受並顯示文字。在最新版本的 JavaFX 中,它僅接受單行輸入。
與文字欄位類似,密碼欄位接受文字,但它不會顯示給定的文字,而是透過顯示回顯字串來隱藏輸入的字元。
在 JavaFX 中,javafx.scene.control.PasswordField 表示密碼欄位,它繼承了 Text 類。要建立密碼欄位,你需要例項化此類。
此類從其父類TextInputControl繼承了一個名為text的屬性。此屬性儲存了當前密碼欄位的內容。你可以使用getText()方法檢索此資料。
示例
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; 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.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class PasswordFieldVerification extends Application { public void start(Stage stage) { //Creating nodes TextField textField = new TextField(); PasswordField pwdField = new PasswordField(); Button button = new Button("Submit"); button.setTranslateX(250); button.setTranslateY(75); //Creating labels Label label1 = new Label("Name: "); Label label2 = new Label("Password: "); //Setting the message with read data Text text = new Text(""); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 10); text.setFont(font); text.setTranslateX(15); text.setTranslateY(125); text.setFill(Color.BROWN); //Displaying the message button.setOnAction(e -> { //Retrieving data String name = textField.getText(); String pwd = pwdField.getText(); if(pwd.equals("abc123")) { text.setText("Hello "+name+" welcome to Tutorialspoint"); } else { text.setText("Wrong password try again"); } }); //Adding labels for nodes HBox box = new HBox(5); box.setPadding(new Insets(25, 5 , 5, 50)); box.getChildren().addAll(label1, textField, label2, pwdField); Group root = new Group(box, button, text); //Setting the stage Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Password Field Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
輸出
輸入的密碼 - mypassword
輸入的密碼 - abc123
廣告