我正在尝试学习Javafx,并且在警报框交互方面遇到一些麻烦。我的代码非常简单:
public static void display(String title, String message) {
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
Label label = new Label();
label.setText(message);
Button closeButton = new Button("Close the window");
closeButton.setOnAction(e -> window.close());
VBox layout = new VBox(10);
layout.getChildren().addAll(label, closeButton);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
}
问题是我仍然可以与用于打开警报框的初始窗口进行某种程度的交互,主要是单击初始窗口将其置于警报框的前面。据我了解,这不应该发生。这是演示问题的gif文件:https://gyazo.com/0c2b69ec39f849227560fbdf2099c07c
这是我的代码,它调用了警报框
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Alert Box test");
button = new Button("Click me");
button.setOnAction(e -> AlertBox.display("New Alert", "Don't forget to close this window!"));
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.show();
}
我在这里做错什么,还是仅仅是预期的行为?