如何在静态函数中等待多个 JavaFX Windows 的结果?
How to wait for a result with multiple JavaFX Windows in a static function?
我的问题是,我如何轻松地等待 中的 window 结果,用户正在输入一些信息?
基本问题:
public static char[] getPassword(String message) {
run = true;
showWindow(message); // JFrame or FX-Window
/* DOES NOT WAIT HERE !!! */
// Working on the insert data of "password1"
if (pwMatches()) {
return password1;
} else {
return null;
}
}
https://pastebin.com/06itDs1X - 只有伪代码,未经测试!真正的代码太复杂了。
任务:
- FX-Window是运行(主舞台)
- 用户应在一个阶段(pw 阶段)输入新密码
- 输入密码后,进入主关卡
问题:
- 主阶段不等待,在输入密码之前继续
- 简单的演示代码:https://pastebin.com/560v1wmd
结果
main 1
main 2
s1
s2
解决办法:
- 1:https://pastebin.com/kCAep2gr
- 2: https://pastebin.com/MSJ0kHfU
结果顺序正确
main 1
s1
s2
main 2
问题:
解决方案 1 需要对代码进行大量修改。
- 我用过这个,但会要求更好的解决方案还是最好的?
解决方案 2 不适用于 JavaFX(线程不是 FX-Thread)
问题:
JavaFX 有任何简单的解决方案还是解决方案 1 是最好的?
此示例应用会暂停主舞台上的交互,直到输入密码。密码是 password
。 注:passwordStage.initModality(Modality.APPLICATION_MODAL);//This is important if you don't want the user to interact with other windows
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* @author blj0011
*/
public class JavaFXApplication22 extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Stage passwordStage = getPasswordStage();
passwordStage.initModality(Modality.APPLICATION_MODAL);//This is important if you don't want the user to interact with other windows
passwordStage.initOwner(primaryStage);
passwordStage.showAndWait();
}
});
BorderPane root = new BorderPane();
root.setCenter(new StackPane(new Label("Main Center")));
root.setTop(new StackPane(new Label("Main Screen")));
root.setRight(new StackPane(new Label("Main Right")));
root.setLeft(new StackPane(new Label("Main Left")));
root.setBottom(new StackPane(btn));
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public Stage getPasswordStage()
{
Stage stage = new Stage();
TextField textField = new TextField();
textField.setPromptText("Enter password here");
Button button = new Button("login");
button.setOnAction(e -> {
if(textField.getText().equals("password"))
{
stage.close();
}
});
VBox vbox = new VBox(new StackPane(textField), new StackPane(button));
StackPane stackPane = new StackPane(vbox);
Scene scene = new Scene(stackPane,300, 200);
stage.setTitle("Login Screen");
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
return stage;
}
}
我的问题是,我如何轻松地等待 中的 window 结果,用户正在输入一些信息?
基本问题:
public static char[] getPassword(String message) {
run = true;
showWindow(message); // JFrame or FX-Window
/* DOES NOT WAIT HERE !!! */
// Working on the insert data of "password1"
if (pwMatches()) {
return password1;
} else {
return null;
}
}
https://pastebin.com/06itDs1X - 只有伪代码,未经测试!真正的代码太复杂了。
任务:
- FX-Window是运行(主舞台)
- 用户应在一个阶段(pw 阶段)输入新密码
- 输入密码后,进入主关卡
问题:
- 主阶段不等待,在输入密码之前继续
- 简单的演示代码:https://pastebin.com/560v1wmd
结果
main 1
main 2
s1
s2
解决办法:
- 1:https://pastebin.com/kCAep2gr
- 2: https://pastebin.com/MSJ0kHfU
结果顺序正确
main 1
s1
s2
main 2
问题:
解决方案 1 需要对代码进行大量修改。
- 我用过这个,但会要求更好的解决方案还是最好的?
解决方案 2 不适用于 JavaFX(线程不是 FX-Thread)
问题:
JavaFX 有任何简单的解决方案还是解决方案 1 是最好的?
此示例应用会暂停主舞台上的交互,直到输入密码。密码是 password
。 注:passwordStage.initModality(Modality.APPLICATION_MODAL);//This is important if you don't want the user to interact with other windows
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* @author blj0011
*/
public class JavaFXApplication22 extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Stage passwordStage = getPasswordStage();
passwordStage.initModality(Modality.APPLICATION_MODAL);//This is important if you don't want the user to interact with other windows
passwordStage.initOwner(primaryStage);
passwordStage.showAndWait();
}
});
BorderPane root = new BorderPane();
root.setCenter(new StackPane(new Label("Main Center")));
root.setTop(new StackPane(new Label("Main Screen")));
root.setRight(new StackPane(new Label("Main Right")));
root.setLeft(new StackPane(new Label("Main Left")));
root.setBottom(new StackPane(btn));
Scene scene = new Scene(root, 500, 400);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public Stage getPasswordStage()
{
Stage stage = new Stage();
TextField textField = new TextField();
textField.setPromptText("Enter password here");
Button button = new Button("login");
button.setOnAction(e -> {
if(textField.getText().equals("password"))
{
stage.close();
}
});
VBox vbox = new VBox(new StackPane(textField), new StackPane(button));
StackPane stackPane = new StackPane(vbox);
Scene scene = new Scene(stackPane,300, 200);
stage.setTitle("Login Screen");
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
return stage;
}
}