如何通过鼠标点击退出JavaFX全屏舞台?触屏KIOSK全屏退出方法咨询
Hey there! Let's break down your two questions about exiting JavaFX fullscreen mode with practical, working code examples.
1. How to Exit JavaFX Fullscreen Stage on Mouse Click
The simplest way to trigger a fullscreen exit on any mouse click is to add a MouseClicked event listener to your scene's root node (or the entire scene itself). Here's a quick implementation:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.scene.input.MouseEvent; public class FullscreenExitOnClick extends Application { @Override public void start(Stage stage) { StackPane root = new StackPane(); Scene scene = new Scene(root, 800, 600); // Enable fullscreen mode stage.setFullScreen(true); // Add mouse click listener to exit fullscreen root.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { stage.setFullScreen(false); event.consume(); // Optional: Prevent event from propagating further }); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
If you only want to exit when clicking a specific area (like an "Exit Fullscreen" button), just attach the listener to that button instead of the root node.
2. Can We Exit Locked Fullscreen via Mouse Click or Touch/Kiosk Input?
Absolutely! Your existing stage.setFullScreenExitKeyCombination(KeyCombination.CONTROL_DOWN); sets a keyboard shortcut, but you can add additional triggers for mouse clicks or touch events (perfect for kiosk setups).
Here's an example that supports both mouse clicks and touch screen interactions to exit fullscreen:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.scene.input.MouseEvent; import javafx.scene.input.TouchEvent; import javafx.scene.input.KeyCombination; public class KioskFullscreenExit extends Application { @Override public void start(Stage stage) { StackPane root = new StackPane(); Scene scene = new Scene(root, 1024, 768); // Enable fullscreen and set your existing keyboard exit shortcut stage.setFullScreen(true); stage.setFullScreenExitKeyCombination(KeyCombination.CONTROL_DOWN); // Exit on mouse click root.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> { stage.setFullScreen(false); }); // Exit on touch screen release (for kiosk/touch devices) root.addEventHandler(TouchEvent.TOUCH_RELEASED, event -> { stage.setFullScreen(false); event.consume(); }); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
A couple of notes for kiosk scenarios:
- Make sure your root node covers the entire screen (most layout panes like
StackPanedo this by default) so all clicks/touches are captured. - If you have child nodes that consume mouse/touch events, you might need to adjust event handling (e.g., use
addEventFilterinstead ofaddEventHandlerto catch events before they reach child nodes).
内容的提问来源于stack exchange,提问作者Shikhs Sachdeva

