键盘操作事件在 JavaFX 中不起作用

Keyboard Action Events not working in JavaFX

我用几种形状创建了一辆汽车,并使用 PathTransition 让它从窗格的一侧移动到另一侧。我正在尝试通过按键(向上和向下箭头)实现动作事件来加速或减慢汽车。为什么键盘事件不工作,即使我有鼠标事件工作?

public class AnimatedCar extends Application {

    @Override
    public void start(Stage mainStage) throws Exception {
        Pane pane = new Pane();
        Group root = new Group();

        // Create a line for the rectangle path
        Line line = new Line();
        line.setStartX(25);
        line.setStartY(290);
        line.setEndX(275);
        line.setEndY(290);
        line.setStroke(Color.TRANSPARENT);
        pane.getChildren().add(line);

        // Create the Back Wheel
        Circle backWheel = new Circle();
        backWheel.setRadius(5);
        backWheel.setStroke(Color.BLACK);
        backWheel.setFill(Color.BLACK);
        backWheel.setCenterX(pane.getLayoutBounds().getWidth() + 20);
        backWheel.setCenterY(pane.getLayoutBounds().getHeight() + 295);
        root.getChildren().add(backWheel);

        // Create the front wheel
        Circle frontWheel = new Circle();
        frontWheel.setRadius(5);
        frontWheel.setStroke(Color.BLACK);
        frontWheel.setFill(Color.BLACK);
        frontWheel.setCenterX(pane.getLayoutBounds().getWidth() + 40);
        frontWheel.setCenterY(pane.getLayoutBounds().getHeight() + 295);
        root.getChildren().add(frontWheel);

        // Create the rectangle part of the car body
        Rectangle rec = new Rectangle(5, 280, 50, 10);
        rec.setStroke(Color.BLUE);
        rec.setFill(Color.BLUE);
        root.getChildren().add(rec);

        // Create polygon for the top trapezoid
        Polygon trapezoid = new Polygon();
        trapezoid.getPoints().addAll(new Double[] { 15.0, 280.0, 20.0, 270.0, 40.0, 270.0, 45.0, 280.0 });
        trapezoid.setFill(Color.BLUE);
        trapezoid.setStroke(Color.BLUE);
        root.getChildren().add(trapezoid);

        // connect the shapes together
        pane.getChildren().add(root);

        // Create a path transition for the rectanagle
        PathTransition pt = new PathTransition();
        pt.setRate(0.25);
        pt.setPath(line);
        pt.setNode(root);
        pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        pt.setCycleCount(Timeline.INDEFINITE);
        pt.setAutoReverse(false);
        pt.play();

        // Create the pause action on the pressed left mouse button
        pane.setOnMousePressed(e -> {
            pt.getStatus();
            if (Status.RUNNING != null)
                pt.pause();
        });
        pane.setOnMouseReleased(e -> {
            pt.getStatus();
            if (Status.STOPPED != null)
                pt.play();

        });

        // Action events for key presses
        pane.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.UP) {
                pt.setRate((pt.getRate() + .03));
            }
        });

        pane.setOnKeyTyped(e -> {
            if (e.getCode() == KeyCode.DOWN) {
                pt.setRate(pt.getRate() - 0.03);
            }
        });

        // Put everything together!
        Scene scene = new Scene(pane, 305, 305);
        mainStage.setTitle("Racecar");
        mainStage.setScene(scene);
        mainStage.show();

    }

    // Main Method to start the program
    public static void main(String[] args) {
        launch(args);
    }

}

窗格没有焦点,因此关键侦听器不起作用。尝试在舞台可见后请求焦点。

mainStage.show();
pane.requestFocus();

此外,您可以将上下按键的逻辑移动到一个方法中。

pane.setOnKeyPressed(e -> {
    if (e.getCode() == KeyCode.UP) {
        pt.setRate((pt.getRate() + .03));
    } else if (e.getCode() == KeyCode.DOWN) {
        pt.setRate(pt.getRate() - 0.03);
    }
});