更新#2
大家好,lemme打破了我的想法。
rootButton
)的primaryStage,用于打开secondaryStage。closeButton
)的secondaryStage,用于关闭此secondaryStage。closeButton.setCancelButton(true);
让ESC键在secondaryStage上按下closeButton
。因此,closeButton以某种方式跳过Alert对话框弹出窗口,而不是直接导致
if (result.get() == ButtonType.CANCEL) {
System.out.println("Cancel was pressed.");
}
那么ESC键事件是否可能从secondaryStage传递到Alert对话框?如果是这种情况,如何和哪里在“警报”对话框中正确使用此键事件?
当前代码(在GoXr3Plus的帮助下格式化):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Closing extends Application {
// handle primaryStage
@Override
public void start(Stage primaryStage) {
Button rootButton = new Button("show dialog");
// rootButton
rootButton.setOnAction(ac -> {
// CloseButton
Button closeButton = new Button("Close");
closeButton.setCancelButton(true); // make this cancel button
closeButton.setOnAction(a -> {
// Initialize the alert
Alert alert = new Alert(AlertType.CONFIRMATION, "You really want to quit?");
// Show the alert
alert.showAndWait().ifPresent(result -> {
if (result == ButtonType.OK)
System.out.println("OK was pressed.");
else if (result == ButtonType.CANCEL)
System.out.println("Cancel was pressed.");
});
closeButton.getScene().getWindow().hide();
});
StackPane dialogPane = new StackPane(closeButton);
Stage secondaryStage = new Stage();
secondaryStage.setScene(new Scene(dialogPane, 200, 200));
secondaryStage.showAndWait();
});
StackPane rootPane = new StackPane(rootButton);
Scene scene = new Scene(rootPane, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
您可能需要发布所有代码或发布mvce,因为您已发布的代码完全适合我。看起来好像你在某种程度上连续两次紧急逃生。
尝试将setOnAction
方法更改为以下内容,并检查输出的标准输出。
btnCancel.setOnAction((ActionEvent event) -> {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setContentText("You really want to quit?");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent()) {
if (result.get() == ButtonType.OK) {
System.out.println("OK was pressed.");
}
if (result.get() == ButtonType.CANCEL) {
System.out.println("Cancel was pressed.");
}
}
primaryStage.close();
});
答案 1 :(得分:0)
你的代码没有编译所以我修改它看起来更好并添加了一些评论。让我知道它是否符合你的要求:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Closing extends Application {
// handle primaryStage
@Override
public void start(Stage primaryStage) {
Button rootButton = new Button("show dialog");
// rootButton
rootButton.setOnAction(ac -> {
// CloseButton
Button closeButton = new Button("Close");
closeButton.setCancelButton(true); // make this cancel button
closeButton.setOnAction(a -> {
// Initialize the alert
Alert alert = new Alert(AlertType.CONFIRMATION, "You really want to quit?");
// Show the alert
alert.showAndWait().ifPresent(result -> {
if (result == ButtonType.OK)
System.out.println("OK was pressed.");
else if (result == ButtonType.CANCEL)
System.out.println("Cancel was pressed.");
});
closeButton.getScene().getWindow().hide();
});
StackPane dialogPane = new StackPane(closeButton);
Stage secondaryStage = new Stage();
secondaryStage.setScene(new Scene(dialogPane, 200, 200));
secondaryStage.showAndWait();
});
StackPane rootPane = new StackPane(rootButton);
Scene scene = new Scene(rootPane, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 2 :(得分:0)