禁用舞台按钮X. [英] Disable stage button X

查看:179
本文介绍了禁用舞台按钮X.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很快就到了,因为我没有一个非常流利的英语,抱歉。

I'm going fast to the point because I haven't a very fluent english, sorry.

我正在学习javafx,我想要点击关闭后X的窗口出现警告。我知道这是在我在窗口中间创建的按钮中执行此操作,但我不知道如何控制用户何时按X关闭程序。谢谢

I'm learning javafx and I want when I click the X's windows after close it appear a warning. I know to do this in a button created by me in the middle of the windows, but I don't know how to control when the user press the X to close the programm. Thanks

推荐答案

你不应该关注X,而应该关注像这样的常见关闭请求:

You should not focus on the X, but instead on the common close request like this:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {

        // consume event
        event.consume();

        // show close dialog
        Alert alert = new Alert(AlertType.CONFIRMATION);
        alert.setTitle("Close Confirmation");
        alert.setHeaderText("Do you really want to quit?");
        alert.initOwner( primaryStage);

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

这篇关于禁用舞台按钮X.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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