我正在尝试将键盘事件分配给特定场景上的特定按键。当单击前一个场景上的按钮时,将加载此场景。
我的问题是:我在哪里将此事件分配给按键?我已经尝试为所述场景的控制器类实现可初始化接口,但是当我尝试在initialize()方法中分配事件(如下面的代码所示)时,我得到一个空指针异常,因为场景显然是,无效的。
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.pane.getScene().setOnKeyPressed(e -> {
if(e.getCode()==KeyCode.E) {
shoot();
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试这样做,而不是
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
this.pane.setOnKeyPressed(e -> {
if(e.getCode()==KeyCode.E) {
shoot();
}
});
}
Run Code Online (Sandbox Code Playgroud)
场景会加载,但按 E 键不会发生任何事情。
场景是用 SceneBuilder 构建的。
通常最好在Scene. 请注意,控制器的initialize()方法是在FXMLLoader.load(...)调用期间调用的,当然是在将 FXML 文件中定义的 UI 添加到场景之前。getScene()因此,对该方法的调用initialize()将返回null。
最简单的方法可能只是在加载 FXML 的代码中的场景上注册关键事件侦听器(通常在这里您将引用Scene),然后调用控制器上的方法:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
哪里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();
}
}
Run Code Online (Sandbox Code Playgroud)
并且,为了完整起见,Game.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 {
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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望将所有内容保留在控制器中,则可以在观察当前场景时做一些相当难看的工作,在设置新场景时添加处理程序。(为了完整起见,如果从场景中删除 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();
}
}
Run Code Online (Sandbox Code Playgroud)
当然,您的 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();
}
}
Run Code Online (Sandbox Code Playgroud)