繁体   English   中英

用户尝试在JavaFx中使用setOnCloseRequest关闭应用程序时的警报框

[英]Alert Box For When User Attempts to close application using setOnCloseRequest in JavaFx

我试图在退出之前提示用户确认他们想要关闭程序。 如果任务仍在执行,我想确认仍然希望退出或让他们有机会在退出之前完成任务。 我使用了setOnCloseRequest,但它没有用。 我使用了event.consume,似乎只是禁用了[x]按钮。 任何建议表示赞赏。 我在这里发现了一个类似的问题对我不起作用 - > 没有函数的JavaFX stage.setOnCloseRequest?

Public class Sample extends Application {
      @Override
    public void start(Stage stage) throws Exception {

    stage.setOnCloseRequest(event -> {
        NewScanView checkScan = new NewScanView();
        boolean isScanning = checkScan.isScanning();

            Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("Close Confirmation");
            alert.setHeaderText("Cancel Creation");
            alert.setContentText("Are you sure you want to cancel creation?");
        if (isWorking == true){

            Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK){
                stage.close();
            }

            if(result.get()==ButtonType.CANCEL){
                alert.close();
            }

        }


    });
    stage.setHeight(600);
    stage.setWidth(800);
    stage.setTitle("Title");
    stage.show();

}

public static void main(String[] args) {
    launch(args);
}

}

这个答案是基于Javafx内部关闭请求的答案。 这个问题与这个问题不同,但答案非常相似。

主要 对话

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
import javafx.stage.WindowEvent;

import java.util.Optional;

public class CloseConfirm extends Application {

    private Stage mainStage;

    @Override
    public void start(Stage stage) throws Exception {
        this.mainStage = stage;
        stage.setOnCloseRequest(confirmCloseEventHandler);

        Button closeButton = new Button("Close Application");
        closeButton.setOnAction(event ->
                stage.fireEvent(
                        new WindowEvent(
                                stage,
                                WindowEvent.WINDOW_CLOSE_REQUEST
                        )
                )
        );

        StackPane layout = new StackPane(closeButton);
        layout.setPadding(new Insets(10));

        stage.setScene(new Scene(layout));
        stage.show();
    }

    private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
        Alert closeConfirmation = new Alert(
                Alert.AlertType.CONFIRMATION,
                "Are you sure you want to exit?"
        );
        Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
                ButtonType.OK
        );
        exitButton.setText("Exit");
        closeConfirmation.setHeaderText("Confirm Exit");
        closeConfirmation.initModality(Modality.APPLICATION_MODAL);
        closeConfirmation.initOwner(mainStage);

        // normally, you would just use the default alert positioning,
        // but for this simple sample the main stage is small,
        // so explicitly position the alert so that the main window can still be seen.
        closeConfirmation.setX(mainStage.getX());
        closeConfirmation.setY(mainStage.getY() + mainStage.getHeight());

        Optional<ButtonType> closeResponse = closeConfirmation.showAndWait();
        if (!ButtonType.OK.equals(closeResponse.get())) {
            event.consume();
        }
    };

    public static void main(String[] args) {
        launch(args);
    }

}

答案确实使用了event.consume() ,你说你尝试过并且没有为你工作(虽然我不知道为什么不能,因为它对我来说这个示例代码很合适)。

尝试创建一个弹出框(如果没有),单击退出时将提示用户。 如果他们选择“Ok”然后调用exit方法,否则返回到你正在做的任何事情。

这是一个网页,其中包含一个示例,说明如何编写弹出文件,因为文档完全是垃圾。 https://gist.github.com/jewelsea/1926196

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM