Javafx 简明教程

JavaFX - PasswordField

文本字段允许输入和显示文本。使用它,我们可以接受用户的输入并将其读取到我们的应用程序。类似于文本字段, password field 接受文本,但不会显示指定的文本,而是通过显示回显字符串来隐藏所键入的字符,如下图所示 −

The text field accepts and displays the text. Using this we can accept input from the user and read it to our application. Similar to the text field, a password field accepts text but instead of displaying the given text, it hides the typed characters by displaying an echo string as shown in the below figure −

sample paswrdfield

PasswordField in JavaFX

在 JavaFX 中,名为 PasswordField 的类表示一个密码字段,该密码字段属于 javafx.scene.control 软件包。使用它,我们可以接受用户的输入并将其读取到我们的应用程序中。此类继承于 Text 类。要创建一个密码字段,我们需要使用以下构造函数实例化此类 −

In JavaFX, the class named PasswordField represents a password field which is belongs to the javafx.scene.control package. Using this we can accept input from the user and read it to our application. This class inherits the Text class. To create a password field, we need to instantiate this class by using the below constructor −

  1. PasswordField() − This is the default constructor which will create an empty password field.

在 JavaFX 中创建一个密码字段时,我们的第一步将使用其默认构造函数实例化 PasswordField 类。接下来,通过将 PasswordField 对象传递给其构造函数来定义一个布局窗格,例如 Vbox 或 Hbox。最后,设置场景和舞台以在屏幕上显示密码字段。

While creating a password field in JavaFX, our first step would be instantiating the PasswordField class by using its default constructor. Next, define a layout pane, such as Vbox or Hbox by passing the PasswordField object to its constructor. Lastly, set the scene and stage to display the password field on the screen.

Example

以下 JavaFX 程序演示了如何在 JavaFX 应用程序中创建一个密码字段。将此代码保存在名为 JavafxPsswrdfld.java 的文件中。

The following JavaFX program demonstrates how to create a password field in JavaFX application. Save this code in a file with the name JavafxPsswrdfld.java.

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 JavafxPsswrdfld 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);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPsswrdfld.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPsswrdfld

当我们执行上述代码时,它将生成以下输出。

When we execute the above code, it will generate the following output.

passwordfield output