按代码关闭fxml窗口,javafx

时间:2012-11-26 14:36:41

标签: java user-interface javafx fxml

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

但它不起作用!

非常感谢所有帮助。谢谢!

9 个答案:

答案 0 :(得分:130)

  1. 如果您还没有:<Button fx:id="closeButton" onAction="#closeButtonAction">
  2. ,请将关闭按钮设为fx:id
  3. 在您的控制器类中:

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

答案 1 :(得分:18)

如果您有一个扩展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());
 }

答案 2 :(得分:8)

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

答案 3 :(得分:1)

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

private void on_btnClose_clicked(ActionEvent actionEvent) {

        Window window = getScene().getWindow();   

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

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

答案 4 :(得分:1)

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

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

最后,我找到了解决方法

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

答案 7 :(得分:0)

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

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

答案 8 :(得分:-2)

你可以用

来实现这一点
@FXML
private void closeAction(ActionEvent evt){
    System.exit(0);
}

会帮你解决问题。