JavaFx: How to keep the window maximizing?

十年热恋 提交于 2020-01-25 12:49:25

问题


My app, if I click the "maximize button", it will maximize the window. But if I go to another scene(in the same stage), the window will restore to the original size. So, how can I control it to keep maximizing?


回答1:


I used primaryStage.setMaximized(true); after calling show(); method in Java 8 implementation. It keeps other scenes maximizing.




回答2:


I had the same problem. I fixed it creating a root stage with that only contains a menu bar and a tool bar, like this:

I initialized this window in the start method with:

FXMLLoader loader = new FXMLLoader();

            loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            RootController controller= loader.getController();

            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();

Later, when you want to load a FXML file or scene into the root container, you could make it with the next lines in another method:

   FXMLLoader loader = new FXMLLoader();
   loader.setLocation(Main.class.getResource("view/principal.fxml"));
   AnchorPane principalOverview = (AnchorPane) loader.load();
   // Set person overview into the center of root layout.
   rootLayout.setCenter(principalOverview);
   // Get the controller instance
   controllerPrincipal = loader.getController();

in that way every scene you add to the root stage will get the size of the root.




回答3:


This is how I did it:

@FXML
private Button goBtn;

Stage stage = (Stage) goBtn.getScene().getWindow();
Scene scene = goBtn.getScene();
try {
   FXMLLoader loader = new FXMLLoader(getClass().getResource("Activity.fxml"));
   scene.setRoot(loader.load());
   stage.setScene(scene);
   stage.show();
} catch (IOException e) {
   e.printStackTrace();
}


来源:https://stackoverflow.com/questions/32642523/javafx-how-to-keep-the-window-maximizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!