【问题标题】:close fxml window by code, javafx通过代码关闭 fxml 窗口,javafx
【发布时间】:2023-04-04 06:38:01
【问题描述】:

我需要通过控制器中的代码关闭当前的fxml窗口

我知道 stage.close() 或 stage.hide() 在 fx 中这样做

如何在 fxml 中实现这一点?我试过了

private void on_btnClose_clicked(ActionEvent actionEvent) {
        Parent root = FXMLLoader.load(getClass().getResource("currentWindow.fxml"));    
        Scene scene = new Scene(root);

        Stage stage = new Stage();            
        stage.setScene(scene);
        stage.show();
}

但它不起作用!

我们将不胜感激所有帮助。谢谢!

【问题讨论】:

    标签: java user-interface javafx fxml


    【解决方案1】:

    如果您不想使用 fxml 链接方法过度扩展您的控制器,您可以执行以下操作:

    你必须给它一个 fx:id。例如“closeButton”。在您的 FXML 文件中应该如下所示:

    <Button fx:id="closeButton" layoutX="876.0" layoutY="74.0" mnemonicParsing="false" textAlignment="CENTER">
    

    然后你可以在你的初始化方法或任何你想要的地方对按钮进行编码:

    @FXML
    Button closeButton
    
    public initialize(){
        closeButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent e){
                ((Stage) closeButton.getScence().getWindow()).close();
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      隐藏不会关闭窗口,只是进入可见模式。 最好的解决方案是:

      @FXML
      private void exitButtonOnAction(ActionEvent event){
              ((Stage)(((Button)event.getSource()).getScene().getWindow())).close();      
      }
      

      【讨论】:

        【解决方案3】:

        终于找到了解决办法

         Window window =   ((Node)(event.getSource())).getScene().getWindow(); 
                    if (window instanceof Stage){
                        ((Stage) window).close();
                    }
        

        【讨论】:

          【解决方案4】:

          如果您有一个扩展javafx.application.Application; 的窗口,您可以使用以下方法。 (这将关闭整个应用程序,而不仅仅是窗口。我误解了 OP,感谢评论者指出)。

          Platform.exit();
          

          例子:

          public class MainGUI extends Application {
          .........
          
          Button exitButton = new Button("Exit");
          exitButton.setOnAction(new ExitButtonListener());
          .........
          
          public class ExitButtonListener implements EventHandler<ActionEvent> {
          
            @Override
            public void handle(ActionEvent arg0) {
              Platform.exit();
            }
          }
          

          为 Java 8 的美而编辑:

           public class MainGUI extends Application {
              .........
          
              Button exitButton = new Button("Exit");
              exitButton.setOnAction(actionEvent -> Platform.exit());
           }
          

          【讨论】:

          • Platform.exit() 将关闭所有阶段并关闭整个应用程序。这可能不是我们想要的。
          • 原发帖人问的是如何关闭窗口(舞台),而不是应用程序。
          【解决方案5】:
          stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
              public void handle(WindowEvent we) {                        
                  Platform.setImplicitExit(false);
                  stage.close();
              }
          });
          

          相当于hide。所以下次打开它时,只需检查stage 对象是否退出即可。如果退出,您只需show()(stage.show()) 调用。否则,你必须开始舞台。

          【讨论】:

            【解决方案6】:

            我找到了一个不需要触发事件的不错的解决方案:

            @FXML
            private Button cancelButton;
            
            close(new Event(cancelButton, stage, null));
            
            @FXML
            private void close(Event event) {
                ((Node)(event.getSource())).getScene().getWindow().hide();                      
            }
            

            【讨论】:

              【解决方案7】:

              在收到来自接受的答案的NullPointerException 后,我通过以下方式实现了这一点。

              在我的 FXML 中:

              <Button onMouseClicked="#onMouseClickedCancelBtn" text="Cancel">
              

              在我的Controller 班级:

              @FXML public void onMouseClickedCancelBtn(InputEvent e) {
                  final Node source = (Node) e.getSource();
                  final Stage stage = (Stage) source.getScene().getWindow();
                  stage.close();
              }
              

              【讨论】:

                【解决方案8】:
                1. 给你的关闭按钮一个 fx:id,如果你还没有:&lt;Button fx:id="closeButton" onAction="#closeButtonAction"&gt;
                2. 在你的控制器类中:

                  @FXML private javafx.scene.control.Button closeButton;
                  
                  @FXML
                  private void closeButtonAction(){
                      // get a handle to the stage
                      Stage stage = (Stage) closeButton.getScene().getWindow();
                      // do what you have to do
                      stage.close();
                  }
                  

                【讨论】:

                • 这种方法对我不起作用。我收到了NullPointerException
                • 令人惊叹的作品! ,以防万一,人们记得检查您的 Button 名称(本例为 closeButton)和您的 onClick 名称(本例为 closeButtonAction)
                • 使用此代码时出现此错误。线程“JavaFX 应用程序线程”java.lang.ClassCastException 中的异常:com.sun.javafx.stage.EmbeddedWindow 无法转换为 javafx.stage.Stage
                • @ZinMinn 如果你想关闭应用程序,只需调用 Platform.exit()。关闭一个阶段与关闭应用程序不同(您可以有多个阶段)。
                • 此解决方案非常适合关闭辅助窗口。 closeButton 必须是要关闭的窗口中的按钮,并且方法 closeButtonAction() 也必须在同一类中。
                【解决方案9】:

                我不确定这是否是最好的方法(或者是否有效),但您可以尝试:

                private void on_btnClose_clicked(ActionEvent actionEvent) {
                
                        Window window = getScene().getWindow();   
                
                        if (window instanceof Stage){
                            ((Stage) window).close();
                        }
                }
                

                (假设你的控制器是一个Node。否则你必须先获取该节点(getScene()是Node的一个方法)

                【讨论】:

                  猜你喜欢
                  • 2013-04-27
                  • 2011-06-21
                  • 2013-11-04
                  • 2012-07-29
                  • 1970-01-01
                  • 2012-04-30
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-09-14
                  相关资源
                  最近更新 更多