私がやろうとしていることは、メグがここで話していることとまったく同じだと思います: JavaFX2 : Closing a stage (substage) from within its inside
JewelSea の回答を実装しようとすると、「非静的メソッド getSource() は静的コンテキストから参照できません」というメッセージが表示されます。
そこで、Scene Builder で作成したセカンダリ ウィンドウ (シーン) を、基本的に 1 つの機能 (ボタンを 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() メソッドをどこに置くべきですか?