EventHandler中句柄(Event)出错

时间:2018-06-04 14:45:28

标签: javafx event-handling eventhandler mouseclick-event

我是javafx的新手,我想打印鼠标点击的坐标。 我已经看过一些例子,但我在EventHandler系列中遇到了一个我不明白的错误,它说:

anonymous javafxapplication2.JavaFXApplication2$1 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);
    }

1 个答案:

答案 0 :(得分:1)

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

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

    });