您可以在指定功能的位置添加一个,例如EventHandler:primaryStage
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("View.fxml"));
AnchorPane pane = loader.load();
primaryStage.setScene(new Scene(pane, 400, 400));
primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
if (KeyCode.F11.equals(event.getCode())) {
primaryStage.setFullScreen(!primaryStage.isFullScreen());
}
});
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
只是为了完整:
View.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.testfullscreen.Controller">
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
控制器:
public class Controller implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Run Code Online (Sandbox Code Playgroud)
我没有实现网络视图,但它应该适用于任何场景。