我正在尝试将键盘事件分配给特定场景上的特定按键。单击前一个场景上的按钮时会加载此场景。
我的问题是:我应该在哪里将此事件分配给按键?我已经尝试为所述场景的 Controller 类实现可初始化接口(interface),但是当我尝试在initialize()方法中分配事件(如下面的代码所示)时,我得到一个空指针异常,因为场景显然是,空。
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.pane.getScene().setOnKeyPressed(e -> {
if(e.getCode()==KeyCode.E) {
shoot();
}
});
}
如果我尝试这样做,那么
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.pane.setOnKeyPressed(e -> {
if(e.getCode()==KeyCode.E) {
shoot();
}
});
}
场景会加载,但按 E 键不会发生任何事情。
场景是使用 SceneBuilder 构建的。
最佳答案
通常最好在场景
上设置按键事件监听器。请注意, Controller 的 initialize()
方法是在 FXMLLoader.load(...)
调用期间调用的,当然,这是在 FXML 文件中定义的 UI 之前调用的被添加到场景中。因此,在 initialize()
方法中调用 getScene()
将返回 null
。
最简单的方法可能只是在加载 FXML 的代码中的场景中注册关键事件监听器(通常在这里您将引用 Scene
),并且调用 Controller 上的方法:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Game.fxml"));
Parent root = fxmlLoader.load();
GameController controller = fxmlLoader.getController();
Scene scene = new Scene(root);
scene.setOnKeyPressed(e -> controller.shoot());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
其中 GameController
位于
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class GameController {
@FXML
private Circle circle ;
public void shoot() {
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(circle.radiusProperty(), 0)),
new KeyFrame(Duration.seconds(0.5), new KeyValue(circle.radiusProperty(), 200))
);
timeline.setOnFinished(e -> circle.setRadius(0));
timeline.play();
}
}
并且,为了完整起见,Game.fxml 是
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<Pane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" prefWidth="600" prefHeight="600" fx:controller="org.jamesd.examples.keyeventexample.GameController">
<Circle fx:id="circle" centerX="300" centerY="300" radius="0" fill="coral"/>
</Pane>
如果您希望将所有内容保留在 Controller 中,您可以做一些相当丑陋的工作,观察当前场景,在设置新场景时添加处理程序。 (为了完整起见,如果从场景中删除 UI,我会删除处理程序,但这种情况并不常见。)
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class GameController {
@FXML
private Circle circle ;
public void initialize() {
EventHandler<KeyEvent> keyPressListener = e -> shoot();
circle.sceneProperty().addListener((obs, oldScene, newScene) -> {
if (oldScene != null) {
oldScene.removeEventHandler(KeyEvent.KEY_PRESSED, keyPressListener);
}
if (newScene != null) {
newScene.addEventHandler(KeyEvent.KEY_PRESSED, keyPressListener);
}
});
}
public void shoot() {
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(circle.radiusProperty(), 0)),
new KeyFrame(Duration.seconds(0.5), new KeyValue(circle.radiusProperty(), 200))
);
timeline.setOnFinished(e -> circle.setRadius(0));
timeline.play();
}
}
当然,您的 FXML 加载代码不需要做任何额外的工作
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Game.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
关于javafx - JavaFX 中 Controller 类中的 KeyEvent 分配位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62559353/