简体   繁体   English

JavaFx:如何保持窗口最大化?

[英]JavaFx: How to keep the window maximizing?

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? 那么,如何控制它以保持最大化? 在此处输入图片说明

I used primaryStage.setMaximized(true); 我使用primaryStage.setMaximized(true); after calling show(); 调用show(); method in Java 8 implementation. Java 8实现中的方法。 It keeps other scenes maximizing. 它使其他场景最大化。

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: 我在start方法中使用以下命令初始化了该窗口:

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: 稍后,当您要将FXML文件或场景加载到根容器中时,可以使用另一种方法的下几行将其制成:

   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. 这样,您添加到根阶段的每个场景都将获得根的大小。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM