延迟函数的执行,直到从javafx窗口返回字符串值
在我的程序中,用户在外部窗口中输入密码,然后通过函数传递该密码。问题是该函数在用户输入密码之前执行。这是我的密码窗口。Passw已声明为类的静态字符串延迟函数的执行,直到从javafx窗口返回字符串值,java,javafx,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);
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;
}
下面是main中执行hostRun函数的代码
password = enterPassword();
while(password == null)
{
}
hostRun(4000000, '0', index, password, 0);
在这里,我尝试了一个while循环来延迟hostRun的执行,但即使这样也会导致密码窗口崩溃。如何延迟执行hostRun,直到用户输入密码为止?如果您想暂停程序,直到后台关闭,您可以使用:
primaryStage.showAndWait();
这将暂停调用stage的方法,直到它关闭。不需要循环。然后enterPassword()将等待阶段关闭。如果您查看代码,这不是应用程序的主要阶段。它只是您正在用关键字new实例化的阶段的变量名。不相关:无论你想实现什么,使用静态方法/字段都是错误的方法(99.9999%)