我的视图包含一堆扩展Tile
的{{1}}个对象和一个带菜单项的菜单栏。我试图用箭头键控制瓷砖发生的事情,但我无法触发事件。
我无法在视图的场景中添加事件处理程序,因为它在初始化时没有场景。
Stackpane
答案 0 :(得分:0)
给我一个镜头,我添加了这一行view.setOnMouseClicked(event -> view.requestFocus());
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
StackPane view = new StackPane();
view.setPrefSize(200,200);
view.setOnMouseClicked(event -> view.requestFocus());
view.setOnKeyPressed(event -> {
switch (event.getCode().toString()) {
case "UP":
event.consume();
System.out.println("UP");
// model.tryMove(Richting.OMHOOG);
break;
case "DOWN":
event.consume();
System.out.println("DOWN");
// model.tryMove(Richting.LINKS);
break;
case "LEFT":
event.consume();
System.out.println("LEFT");
// model.tryMove(Richting.LINKS);
break;
case "RIGHT":
event.consume();
System.out.println("RIGHT");
// model.tryMove(Richting.RECHTS);
break;
}
// model.checkDone();
});
Scene scene = new Scene(view);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args); }
}