禁用JavaFX的Alt + F4 [英] Disable Alt + F4 for JavaFX

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

问题描述

我需要使用 Alt + F4 键盘快捷键禁用关闭事件。现在,我正在尝试过滤我的场景中的事件以获得此按键并使用它,但没有任何成功,无论如何都会发生关闭事件。以下是我的部分代码:

I need to disable the close event using Alt + F4 keyboard shortcut. For now, I'm trying to filter the events in my Scene for this keypress and consume it, but didn't have any success, the close event happens anyway. Follows bellow part of my code:

scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
    if (event.isAltDown() && event.getCode().equals(KeyCode.F4)) {
        event.consume();
    }
});

primaryStage.setOnCloseRequest((ev) -> System.exit(0));


推荐答案

您可以尝试禁用隐式退出:

You can try to disable the implicit exit:

Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        event.consume();
    }
});

然后创建一个按钮,在点击时关闭应用程序:

and then create a button which will close the app on click:

Button btn = new Button();
btn.setText("Close");
btn.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
       System.exit(0);
    }
});

这篇关于禁用JavaFX的Alt + F4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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