特别要注意第二窗体对应的view的fxml文件路径写法(view和controller都放在各自的包下):
@FXML
protected void show2Action() throws IOException {
Stage stage = new Stage();
stage.setTitle("第二个窗体实验");
AnchorPane pane = FXMLLoader.load(getClass().getResource("../javafxcontroller2.fxml"));//要注意fxml文件的类路径写法(与本JavaFXController.java的相对关系)
Scene scene = new Scene(pane,300,400);
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);//模式窗体
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println("window close event");
if(i++<2){
event.consume();
// i=0;
}
else
i=0;
}
});
stage.show();
// stage.showAndWait(); //等待stage关闭之后才能继续运行
System.out.println("stage.showAndWait");
// stage.show();
}
来源:oschina
链接:https://my.oschina.net/u/2963604/blog/4299086