首页 文章

按代码关闭fxml窗口,javafx

提问于
浏览
37

我需要通过控制器中的代码关闭当前的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();
}

但它不起作用!

所有帮助将不胜感激 . 谢谢!

7 回答

  • 1
    • 给你的关闭按钮一个fx:id,如果你还没有: <Button fx:id="closeButton" onAction="#closeButtonAction">

    • 在您的控制器类中:

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

    如果您有一个扩展 javafx.application.Application; 的窗口,您可以使用以下方法 .

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

    Edit for the beauty of Java 8:

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

    我从接受的答案中收到 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

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

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

    (假设您的控制器是节点 . 否则您必须先获取节点(getScene()是Node的方法)

  • 1

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

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

    你可以简单地实现这一点,

    @FXML
    private void closeAction(ActionEvent evt){
        System.exit(0);
    }
    

    会为你做的伎俩 .

  • 0
    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent we) {                        
            Platform.setImplicitExit(false);
            stage.close();
        }
    });
    

    它相当于 hide . 因此,当您下次打开它时,您只需检查 stage 对象是否已退出 . 如果退出,你只需 show()(stage.show()) 电话 . 否则,你必须开始阶段 .

相关问题