JavaFx close window on pressing esc?

后端 未结 4 831
故里飘歌
故里飘歌 2021-01-12 18:59

Im calling a new stage in my program which I like to close on pressing escape. I did this which gives me a NullPointerException:

    scene.setOnKeyPressed(ne         


        
4条回答
  •  灰色年华
    2021-01-12 19:19

    Add the event handler to the stage/window you want to close on ESC.

    JavaFX 8 style:

        stage.addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
            if (KeyCode.ESCAPE == event.getCode()) {
                stage.close();
            }
        });
    

提交回复
热议问题