繁体   English   中英

通过代码关闭 fxml 窗口,javafx

[英]close fxml window by code, javafx

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

但它不起作用!

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

  1. 给你的关闭按钮一个 fx:id,如果你还没有: <Button fx:id="closeButton" onAction="#closeButtonAction">
  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(); }

如果您有一个扩展javafx.application.Application;的窗口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());
 }

从接受的答案中收到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();
}

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

private void on_btnClose_clicked(ActionEvent actionEvent) {

        Window window = getScene().getWindow();   

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

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

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

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

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

@FXML
private Button cancelButton;

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

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

最后,我找到了解决方案

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

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

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

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

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

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

然后你可以在你的构造函数或任何你想要的地方对按钮进行编码:

@FXML
Button closeButton

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

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

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

会为你做的伎俩。

暂无
暂无

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

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