EventHandler 中的 handle(Event) 错误

Error with handle(Event) in EventHandler

我是 javafx 的新手,我只想打印鼠标点击的坐标。 我看过示例,但 EventHandler 行中有一个我不理解的错误,它说:

anonymous javafxapplication2.JavaFXApplication2 is not abstract and does not override abstract method handle(Event) in EventHandler

有什么问题吗?谢谢!

package javafxapplication2;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Line;
import javafx.stage.Stage;


public class JavaFXApplication2 extends Application {
    @Override
    public void start(Stage primaryStage) {
        Line line = new Line();

        Group root = new Group(line);

        Scene scene = new Scene(root, 800, 800);
        scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println(event.getX());
                System.out.println(event.getY());
            }
        }
        primaryStage.setTitle("Disegna linee");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

通过查看有关 javafx 处理程序的文档 here,我相信您的问题是您需要向新的 eventHandler 提供类型,例如

scene.addEventHandler(MouseEvent.MOUSE_CLICKED, 
    new EventHandler<MouseEvent>() { // Was missing the <MouseEvent>
        @Override
        public void handle(MouseEvent event) { ... };

    });