我想要做的事情我认为正是梅格在这里所说的话:JavaFX2 : Closing a stage (substage) from within itself
当我尝试实现JewelSea的答案时,我得到了“非静态方法getSource()无法从静态上下文中引用。”
所以我在Scene Builder中创建了一个辅助窗口(场景),它有一个基本上只有一个函数的简单控制器类:将按钮绑定到close()事件处理程序。这是我的代码:
public class ProductNotFoundController
implements Initializable {
@FXML // fx:id="closeButton"
private Button closeButton; // Value injected by FXMLLoader
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
closeButton.setOnAction(new EventHandler<ActionEvent> () {
@Override
public void handle(ActionEvent t) {
// ProductNotFound.avisoClose();
Node source;
source = (Node) ActionEvent.getSource();
Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
});
}
}
有人可以告诉我我做错了什么吗?和/或我应该在哪里放置close()方法?