setting/exiting 全屏的相同 KeyCode - JavaFx

Same KeyCode for setting/exiting fullscreen - JavaFx

我用它来 setFullScreen():

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent e) {
            if(e.getCode() == KeyCode.F11) {
                stage.setFullScreen(true);
            }
        }
    });

这行退出它:

stage.setFullScreenExitKeyCombination(new KeyCodeCombination(KeyCode.F11));

如您所见,我想对两者使用相同的 KeyCode (F11)。但是它没有正确退出全屏!我猜,它在退出后立即设置全屏。所以它不会关闭全屏模式。

只需设置一个全屏标志 true/false 即可。

boolean fullScreen = false;            // This is global variable.
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent e) {
        if(e.getCode() == KeyCode.F11) {
            if(fullScreen)
               stage.setFullScreen(true);
             else
               stage.setFullScreen(false);
            // Toggling fullscreen variable after toggling full screen
            fullScreen = !fullScreen;
        }
    }
});

这是对您的问题最简单的回答。

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent e) {
    if(e.getCode() == KeyCode.F11) {
        stage.setFullScreen(!stage.isFullScreen());
    }
}
});