Javafx按钮事件处理程序,无法作为变量解决



在手柄方法中,它说executeButton无法在句柄方法中解决。这是为什么?我是Javafx的新手。我做了所有事情,因为YouTube上的教程向我介绍了。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class BankApplication extends Application implements 
EventHandler<ActionEvent>{
public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
    Button executeButton = new Button("Execute");
    executeButton.setOnAction(this);        
}
@Override
public void handle(ActionEvent event) {
    if (event.getSource() == executeButton) {
    }
}

}

将按钮放入实例变量中,以便可以从事件处理程序访问。

public class BankApplication extends Application implements
    EventHandler<ActionEvent> {
private Button executeButton = new Button("Execute");
public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
    executeButton.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
    if (event.getSource() == executeButton) {
    }
}
}

最新更新