【问题标题】:JavaFx: How to keep the window maximizing?JavaFx:如何保持窗口最大化?
【发布时间】:2015-12-15 01:01:15
【问题描述】:

我的应用程序,如果我单击“最大化按钮”,它将最大化窗口。但是如果我去另一个场景(在同一阶段),窗口会恢复到原来的大小。那么,我怎样才能控制它以保持最大化呢?

【问题讨论】:

  • 考虑保留相同的场景,并替换其root,而不是替换场景本身。

标签: java javafx javafx-2 javafx-8


【解决方案1】:

我在 Java 8 实现中调用 show(); 方法后使用了 primaryStage.setMaximized(true);。它使其他场景最大化。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我修复了它创建一个根阶段,它只包含一个菜单栏和一个工具栏,如下所示:

    我在 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();
    

    稍后,当您想将 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();
    

    这样,您添加到根阶段的每个场景都将获得根的大小。

    【讨论】:

      【解决方案3】:

      我就是这样做的:

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

      【讨论】:

        猜你喜欢
        • 2011-05-17
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多