简体   繁体   English

通过代码关闭 fxml 窗口,javafx

[英]close fxml window by code, javafx

I need to close the current fxml window by code in the controller我需要通过控制器中的代码关闭当前的 fxml 窗口

I know stage.close() or stage.hide() do this in fx我知道 stage.close() 或 stage.hide() 在 fx 中执行此操作

how to implement this in fxml?如何在 fxml 中实现这个? I tried我试过

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

but it doesn't work!但它不起作用!

All help will be greatly appreciated.所有帮助将不胜感激。 Thanks!谢谢!

  1. give your close button an fx:id, if you haven't yet: <Button fx:id="closeButton" onAction="#closeButtonAction">给你的关闭按钮一个 fx:id,如果你还没有: <Button fx:id="closeButton" onAction="#closeButtonAction">
  2. In your controller class:在您的控制器类中:

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

If you have a window which extends javafx.application.Application;如果您有一个扩展javafx.application.Application;的窗口javafx.application.Application; you can use the following method.您可以使用以下方法。 (This will close the whole application, not just the window. I misinterpreted the OP, thanks to the commenters for pointing it out). (这将关闭整个应用程序,而不仅仅是窗口。我误解了 OP,感谢评论者指出它)。

Platform.exit();

Example:例子:

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:为 Java 8 之美进行编辑:

 public class MainGUI extends Application {
    .........

    Button exitButton = new Button("Exit");
    exitButton.setOnAction(actionEvent -> Platform.exit());
 }

I implemented this in the following way after receiving a NullPointerException from the accepted answer.从接受的答案中收到NullPointerException后,我以以下方式实现了这一点。

In my FXML:在我的 FXML 中:

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

In my Controller class:在我的Controller类中:

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

I'm not sure if this is the best way (or if it works), but you could try:我不确定这是否是最好的方法(或者它是否有效),但您可以尝试:

private void on_btnClose_clicked(ActionEvent actionEvent) {

        Window window = getScene().getWindow();   

        if (window instanceof Stage){
            ((Stage) window).close();
        }
}

(Assuming your controller is a Node. Otherwise you have to get the node first (getScene() is a method of Node) (假设你的控制器是一个节点。否则你必须先获取节点(getScene() 是节点的一个方法)

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

It is equivalent to hide .它相当于hide So when you are going to open it next time, you just check if the stage object is exited or not.所以当你下次打开它时,你只需检查stage对象是否退出。 If it is exited, you just show() ie (stage.show()) call.如果它被退出,你只需show()(stage.show())调用。 Otherwise, you have to start the stage.否则,您必须开始舞台。

I found a nice solution which does not need an event to be triggered:我找到了一个很好的解决方案,它不需要触发事件:

@FXML
private Button cancelButton;

close(new Event(cancelButton, stage, null));

@FXML
private void close(Event event) {
    ((Node)(event.getSource())).getScene().getWindow().hide();                      
}

finally, I found a solution最后,我找到了解决方案

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

Hide doesn't close the window, just put in visible mode.隐藏不会关闭窗口,只是进入可见模式。 The best solution was:最好的解决办法是:

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

If you don't want to overspread you your controller with fxml linked methods you can do something like this:如果您不想使用 fxml 链接方法过度扩展您的控制器,您可以执行以下操作:

You have to give it a fx:id.你必须给它一个 fx:id。 "closeButton" for Example.例如“closeButton”。 Should look like this in your FXML file:在您的 FXML 文件中应如下所示:

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

Then you can just code the button in your constructor or wherever you want:然后你可以在你的构造函数或任何你想要的地方对按钮进行编码:

@FXML
Button closeButton

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

you can simply achieve this with, 你可以简单地实现这一点,

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

will do the trick for you. 会为你做的伎俩。

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

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