2015-11-07 90 views
0

我已經用幾個形狀創建了一輛車,並使用PathTransition從窗格的一側移動到另一側。我試圖通過按鍵(上下箭頭)來實施行動事件,以加速或減速汽車。爲什麼鍵盤事件不能工作,即使我有鼠標事件工作?鍵盤操作在JavaFX中不能工作的事件

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); 
    } 

} 

A picture of the window

+1

我的猜測是你的窗格沒有焦點。嘗試在'mainStage.show()'後面調用'pane.requestFocus()'。 – ItachiUchiha

+0

@IchichiUchiha Yup!這工作!謝謝一堆! – CadetFayed

回答

1

窗格沒有焦點,所以關鍵監聽器不工作。在舞臺可見後嘗試請求焦點。

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); 
    } 
});