我目前正在编程自己的小俄罗斯方块,因此我需要KeyEvents。问题是,它不起作用。这是我的控制器的一部分:
public class TetrisFXMLController implements Initializable {
@FXML
private AnchorPane anchorPane;
private Board tetrisBoard;
private BorderPane bpTetris;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
bpTetris = new BorderPane();
bpTetris.setLayoutX(BOARD_X);
bpTetris.setLayoutY(BOARD_Y);
bpTetris.setPrefWidth(BOARD_WIDTH);
bpTetris.setPrefHeight(BOARD_HEIGHT);
anchorPane.getChildren().add(bpTetris);
... some other working init
System.out.println("Init done");
}
// Event Listener on AnchorPane[#anchorPane].onKeyPressed
@FXML
public void onKeyPressedAnchorPane(KeyEvent event) {
System.out.println(event.getCode() + "pressed");
}
// Event Listener on AnchorPane[#anchorPane].onKeyReleased
@FXML
public void onKeyReleasedAnchorPane(KeyEvent event) {
System.out.println(event.getCode() + "released");
}
... some other working Listeners
}
和我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<AnchorPane fx:id="anchorPane" focusTraversable="true" onKeyPressed="#onKeyPressedAnchorPane" onKeyReleased="#onKeyReleasedAnchorPane" prefHeight="538.1" prefWidth="400.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TetrisFXMLController">
<children>
... some children
</children>
</AnchorPane>
谢谢大家!