Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Button 添加按钮后,JavaFX键盘输入停止工作_Button_Javafx_Input - Fatal编程技术网

Button 添加按钮后,JavaFX键盘输入停止工作

Button 添加按钮后,JavaFX键盘输入停止工作,button,javafx,input,Button,Javafx,Input,我正在建立一个游戏引擎作为一个学校的项目。当我将按钮添加到画布所在的同一组时,我无法再使用键盘控制播放器。按钮和其他一切仍然正常工作 我的代码非常庞大,因此这是问题的简化代码: public abstract class GEngine extends Application { public void start(Stage theStage) throws Exception { try { this.stage = theStage;

我正在建立一个游戏引擎作为一个学校的项目。当我将按钮添加到画布所在的同一组时,我无法再使用键盘控制播放器。按钮和其他一切仍然正常工作

我的代码非常庞大,因此这是问题的简化代码:

public abstract class GEngine extends Application {

    public void start(Stage theStage) throws Exception {
        try {
            this.stage = theStage;

            Group root = new Group();

            Scene theScene = new Scene(root);
            stage.setScene(theScene);

            Canvas canvas = new Canvas(windowWidth, windowHeight);
            root.getChildren().add(canvas);

            Button btn = new Button("new");
            btn.setOnMousePressed(e->System.out.println("press"));
            root.getChildren().add(btn);

            Timeline gameLoop = new Timeline();
            gameLoop.setCycleCount(Timeline.INDEFINITE);

            // Handle input
            theScene.setOnKeyPressed(Input::handlePressed);
            theScene.setOnKeyReleased(Input::handleReleased);

            // Control game loop and it's speed
            KeyFrame kf = new KeyFrame(
                Duration.seconds(0.017),                // 60 FPS
                (e)->this.controlGameLoop());

            gameLoop.getKeyFrames().add( kf );
            gameLoop.play();

            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
            stop();
        }
    }
}
可能有一些事情发生在背景上,我只是不明白。如果有人想看,我也可以显示输入类代码,但据我所知,这不是必需的


我曾尝试使用AnchorPane作为主根,并为按钮和画布创建一个单独的组,我将其添加到AnchorPane中,但这毫无帮助。这几乎是我从谷歌找到的唯一解决方案。

添加
btn.setFocusTraversable(false)修复了问题,感谢Luxusproblem提供了答案

尝试添加
btn.setFocusTraversable(false)
您的代码无法编译。请添加MCVE。