如何解除内部关闭请求? [英] How can I fire internal close request?

查看:73
本文介绍了如何解除内部关闭请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX中关闭窗口时遇到问题。

I have trouble when closing a window in JavaFX.

我按照自己的意愿定义了我的 setOnCloseRequest ,当我点击窗口中的x时,它可以正常工作。但是,我还需要一个关闭窗口的按钮,这个 onCloseRequest 必须工作,问题是它没有。事件根本不会发生。

I define my setOnCloseRequest as I wanted and it works when I click the x in the window. However, I also need a button to close the window and this onCloseRequest has to work, the problem is it does not. The event does not fire at all.

我正在使用JavaFX 2.2(Java 7),我注意到 setOnCloseRequest的引用表示关闭窗口外部请求

I am using JavaFX 2.2 (Java 7) and I notice that the reference for setOnCloseRequest says close the window on external request

推荐答案

解决方案

从您的内部关闭请求(按下按钮)触发事件,以便应用程序认为它收到了外部关闭请求。然后,无论请求来自外部事件还是内部事件,您的关闭请求逻辑都可以是相同的。

Fire an event from your internal close request (on the button push), so that the application thinks it received an external close request. Then your close request logic can be identical whether the request came from an external event or an internal one.

private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
        // close event handling logic.
        // consume the event if you wish to cancel the close operation.
}

...

stage.setOnCloseRequest(confirmCloseEventHandler);

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

注意

这是Java 8+解决方案,对于JavaFX 2,您需要转换匿名内部类中的lambda函数,并且无法使用Alert对话框,但需要提供自己的警报对话框系统,如JavaFX 2所做的那样没有内置的。我强烈建议升级到Java 8+而不是使用JavaFX 2.

This is a Java 8+ solution, for JavaFX 2, you will need to convert the lambda functions in anonymous inner classes and will be unable to use the Alert dialog box but will need to provide your own alert dialog system as JavaFX 2 does not feature an in-built one. I strongly recommend upgrading to Java 8+ rather than staying with JavaFX 2.

示例用户界面

示例代码

示例代码将向用户显示关闭确认提醒,如果用户未确认关闭,则取消关闭请求。

The sample code will show the user a close confirmation alert and cancel the close request if the user does not confirm the close.

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);
    }

}

这篇关于如何解除内部关闭请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆