JavaFx: How to keep the window maximizing?

2019-08-27 01:14发布

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? enter image description here

3条回答
三岁会撩人
2楼-- · 2019-08-27 01:43

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

查看更多
Luminary・发光体
3楼-- · 2019-08-27 01:49

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: enter image description here

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.

查看更多
我只想做你的唯一
4楼-- · 2019-08-27 01:53

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();
}
查看更多
登录 后发表回答