JavaFx |密码字段

PasswordField类是JavaFX包的一部分。这是一个屏蔽输入字符的文本字段(输入的字符不会显示给用户)。它允许用户输入单行未格式化文本,因此不允许多行输入。

null

PasswordField类的构造函数:

  1. 密码字段() :创建一个新的密码字段

(PasswordField继承了TextField,因此这里可以使用TextField的所有方法。PasswordField没有单独的方法,所有方法都是从文本字段继承的。)

下面的程序演示了PasswordField类的使用:

  1. Java程序创建一个密码字段 :该程序将创建一个由名称b表示的密码字段。该密码字段将在场景中创建,而场景又将托管在舞台(即顶级JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法,以在场景中附加密码字段,以及代码中(200200)指定的分辨率。最后,调用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);
    }
    }

    
    

    输出 : 图片[1]-JavaFx |密码字段-yiteyi-C++库

  2. 用于创建密码字段并添加事件处理程序的Java程序: 这个程序创建一个由名字b表示的密码字段。我们将创建一个标签,当按下回车键时,该标签将显示密码。我们将创建一个事件处理程序来处理password字段的事件,并使用setOnAction()方法将事件处理程序添加到passwordfield中。PasswordField将在场景中创建,而场景又将托管在一个stage(顶层JavaFX容器)中。函数setTitle()用于为舞台提供标题。然后创建一个标题窗格,在该窗格上调用addChildren()方法在场景中附加密码字段和标签,以及代码中(200200)指定的分辨率。最后,调用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);
    }
    }

    
    

    输出 : 图片[2]-JavaFx |密码字段-yiteyi-C++库

注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享