我有以下代码:
public class Main extends Application {
@FXML
private Button button;
@Override
public void start(Stage primaryStage) {
try {
...
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println(button)
}
});
...
}
@FXML
private void initialize() {
System.out.println(button);
}
}
我运行它,然后点击关闭按钮,这是输出:
Button[id=button, styleClass=button]'button'
null
为什么在setOnCloseRequest中调用按钮时为null?