當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaFX 類 PasswordField用法及代碼示例

PasswordField類是JavaFX軟件包的一部分。它是一個文本字段,可掩蓋輸入的字符(輸入的字符不會顯示給用戶)。它允許用戶輸入單行未格式化的文本,因此不允許multi-line輸入。

PasswordField類的構造函數:

  1. PasswordField():創建一個新的PasswordField

(PasswordField繼承了TextField,因此可以在此處使用TextField的所有方法。password字段沒有單獨的方法,所有方法都從文本字段繼承。)


以下示例程序旨在說明PasswordField類的用法:

  1. Java程序創建密碼字段:此程序創建一個用名稱b表示的PasswordField。 PasswordField將在一個場景內創建,而該場景又將在一個場景(這是頂級JavaFX容器)中托管。函數setTitle()用於為舞台提供標題。然後,創建一個Title-pane,在其上調用addChildren()方法以將PasswordField附加到場景內,以及代碼中的(200,200)指定的分辨率。最後,調用show()方法以顯示最終結果。
    // Java program to create a passwordfield 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.control.Label; 
    import javafx.stage.Stage; 
    public class Passwordfield extends Application 
    { 
          
        // launch the application 
        public void start(Stage s) 
        { 
            // set title for the stage 
            s.setTitle("creating Passwordfield"); 
              
            // create a Passwordfield 
            PasswordField b =new PasswordField(); 
              
            // create a tile pane 
            TilePane r = new TilePane(); 
              
              
            // add password field 
            r.getChildren().add(b); 
              
            // create a scene 
            Scene sc =new Scene(r,200,200); 
              
            // set the scene 
            s.setScene(sc); 
             
            s.show(); 
      
        } 
          
        public static void main(String args[]) 
        { 
            // launch the application 
           launch(args); 
        } 
    }

    輸出

  2. Java程序創建一個passwordfield並添加一個事件處理程序:該程序創建一個由名稱b表示的PasswordField。我們將創建一個標簽,該標簽將在按下Enter鍵時顯示密碼。我們將創建一個事件處理程序,該事件處理程序將處理密碼字段的事件,並將使用setOnAction()方法將該事件處理程序添加至密碼字段。 PasswordField將在一個場景內創建,而該場景又將在一個場景(這是頂級JavaFX容器)中托管。函數setTitle()用於為舞台提供標題。然後,創建一個Title-pane,在其上調用addChildren()方法以將PasswordField和一個標簽附加到場景內部,以及代碼中的(200,200)指定的分辨率。最後,調用show()方法以顯示最終結果。
    // Java program to create a passwordfield and add 
    // a event handler to handle the event of Passwordfield 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.control.Label; 
    import javafx.stage.Stage; 
    public class Passwordfield_1 extends Application 
    { 
          
        // launch the application 
        public void start(Stage s) 
        { 
            // set title for the stage 
            s.setTitle("creating Passwordfield"); 
              
            // create a Passwordfield 
            PasswordField b =new PasswordField(); 
              
            // create a tile pane 
            TilePane r = new TilePane(); 
              
            // create a label 
            Label l = new Label("no Password"); 
              
            // action event 
            EventHandler<ActionEvent> event = new EventHandler<ActionEvent>(){ 
            public void handle(ActionEvent e) 
            { 
                l.setText(b.getText()); 
            } 
            }; 
              
            // when enter is pressed 
            b.setOnAction(event); 
              
            // add password field 
            r.getChildren().add(b); 
            r.getChildren().add(l); 
              
            // create a scene 
            Scene sc =new Scene(r,200,200); 
              
            // set the scene 
            s.setScene(sc); 
             
            s.show(); 
      
        } 
          
        public static void main(String args[]) 
        { 
            //launch the application 
           launch(args); 
        } 
    }

    輸出

注意:以上程序可能無法在在線IDE中運行,請使用離線編譯器。

參考:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/PasswordField.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFx | PasswordField。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。