如何使用 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); } }
輸出
廣告