有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java延迟函数的执行,直到从javafx窗口返回字符串值

在我的程序中,用户在外部窗口中输入密码,然后通过函数传递该密码。问题是,该函数是在用户输入密码之前执行的。这是我的密码窗口。Passw已声明为该类的静态字符串

public static String enterPassword()
{

    Stage primaryStage = new Stage();

    VBox vb = new VBox();

    vb.setPadding(new Insets(10, 0, 0, 10));

    vb.setSpacing(10);

    HBox hb = new HBox();

    hb.setSpacing(10);

    hb.setAlignment(Pos.CENTER_LEFT);

    Label label = new Label("Password");

    final PasswordField pb = new PasswordField();

    final TextField textField = new TextField();

    textField.setManaged(false);

    textField.setVisible(false);

    CheckBox checkBox = new CheckBox("Show/Hide password");

    textField.managedProperty().bind(checkBox.selectedProperty());


    textField.visibleProperty().bind(checkBox.selectedProperty());


    pb.managedProperty().bind(checkBox.selectedProperty().not());

    pb.visibleProperty().bind(checkBox.selectedProperty().not());

    // Bind the textField and passwordField text values 
    bidirectionally.

    textField.textProperty().bindBidirectional(pb.textProperty());

    pb.setOnAction(e -> 
    {

        passw = pb.getText();




       pb.clear();
       primaryStage.hide();

    });

    textField.setOnAction(e -> 
    {

        passw = textField.getText();

        textField.clear();

        primaryStage.hide();
    });

    hb.getChildren().addAll(label, pb, textField, checkBox);
    vb.getChildren().addAll(hb);
    Scene scene = new Scene(vb, 450, 90);
    primaryStage.setScene(scene);


    primaryStage.show();



    return passw;

}

下面是执行hostRun函数的main代码

password = enterPassword();

while(password == null)
{

}

hostRun(4000000, '0', index, password, 0);

在这里,我尝试了一个while循环来延迟hostRun的执行,但即使这样也会导致密码窗口崩溃。在用户输入密码之前,如何延迟hostRun的执行


共 (1) 个答案

  1. # 1 楼答案

    如果你想暂停节目直到舞台关闭,你可以使用:

    primaryStage.showAndWait();
    

    这将暂停调用stage的方法,直到它关闭。不需要循环。然后enterPassword()将等待舞台关闭