JavaFX 按钮不工作

JavaFX button not working

我开始写一个简单的音乐播放器,但是当我测试它时,我发现按钮不起作用。我用简单的 System.out.println("Got to button"); 替换了按钮事件处理程序中的代码,但它没有写入控制台。

按钮的 fxID 是 playButton。 @fxml 声明是:

@FXML
 private Button playButton;

初始化:

 @FXML
 public void initialize() {
    this.bindGuiComponentsToViewModel();
    this.setEventActions();
 }

设置事件操作:

private void setEventActions() {
    this.playButton.setOnAction(event -> this.handlePlayAction());
}

处理程序:

private void handlePlayAction() {
    System.out.println("got to play");
}

在过去的一个小时里,我无法通过 google 找到任何东西,我已经尝试制作 playButton.setDisable(false);,但到目前为止没有任何效果。

非常感谢任何帮助!

为什么不测试常规方式(使用匿名 class 的实现)如果它有效然后按钮被绑定

yourButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
     System.out.println("got to play");
}
});

其他解决方案: -尝试使用Logger -尝试通过在

中添加断点来调试代码
private void handlePlayAction() {
  x  System.out.println("got to play");
}