关闭台下(Closing a substage)

2019-10-18 02:43发布

我想要做的,我认为正是梅格在谈论这里: JavaFX2:内本身关闭一个阶段(个子)

当我尝试实施JewelSea的答案,我得到了“非静态方法的getSource()不能从静态上下文中引用。”

所以,我在现场生成器与具有基本上一个功能简单的控制器类创建了我的第二个窗口(场景):领带的按钮,将关闭()事件处理程序。 下面是我的代码:

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()方法?

Answer 1:

取代ActionEventt , 的getSource为非静态方法。 jewelsea使用actionEvent的实例ActionEvent类。



文章来源: Closing a substage
标签: javafx-2