Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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
如何在窗口之间切换时阻止JavaFx滑块控件重置为其默认值_Java_Javafx_Fxml_Scenebuilder - Fatal编程技术网

如何在窗口之间切换时阻止JavaFx滑块控件重置为其默认值

如何在窗口之间切换时阻止JavaFx滑块控件重置为其默认值,java,javafx,fxml,scenebuilder,Java,Javafx,Fxml,Scenebuilder,我在SceneBuilder中为javafx构建了一系列fxml窗口。当我在设置窗口和主菜单之间切换时,各种音量的滑块将重置为其原始值,但我希望它们保留上次打开窗口时的值 我在initialise和settings中尝试了设置值,并在类中设置了一个变量,该变量被更改并用于在启动滑块时设置滑块,但这也不起作用 FXML 设置控制器 公共类设置控制器{ @FXML//fx:id=soundfxSlider 私有滑块soundfxSlider;//FXMLLoader注入的值 @FXML//fx:id

我在SceneBuilder中为javafx构建了一系列fxml窗口。当我在设置窗口和主菜单之间切换时,各种音量的滑块将重置为其原始值,但我希望它们保留上次打开窗口时的值

我在initialise和settings中尝试了设置值,并在类中设置了一个变量,该变量被更改并用于在启动滑块时设置滑块,但这也不起作用

FXML 设置控制器 公共类设置控制器{ @FXML//fx:id=soundfxSlider 私有滑块soundfxSlider;//FXMLLoader注入的值 @FXML//fx:id=musicSlider 私有滑块musicSlider;//FXMLLoader注入的值 @FXML void updateMusicMouseeEvent事件{ double musicVolume=musicSlider.getValue; //setMusicSlidermusicVolume; 发射器。调整体积体积; } @FXML void updatesFXMouseeEvent事件{ 双音量=soundfxSlider.getValue; //setSoundfxSlidervol; Launcher.adjustSfxvol; } 私有无效设置MusicSliderDouble sliderVal{ musicSlider.setValuesliderVal; } 私有void setSoundfxSliderdouble sfxVal{ soundfxSlider.setValuesfxVal; } @FXML void playTestSfxActionEvent事件{ Launcher.playTestSFX; } @FXML void goBackActionEvent事件{ Launcher.LaunchScreen主菜单; } @FXML//初始化完成后,FXMLLoader将调用此方法 无效初始化{ 断言soundfxSlider!=null:fx:id=\soundfxSlider\未被注入:检查FXML文件“settings.FXML”。; assert musicSlider!=null:fx:id=\musicSlider\未被注入:检查FXML文件“settings.FXML”。; }
如果fxml中的滑块没有任何值,则默认值为0-似乎默认值是强制的,它不记得何时切换窗口。

我相信我理解为什么在打开新窗口或加载新窗口时,滑块上总是会出现默认值

如果您看一下下面的代码,我们会看到您通过加载带有参数名称的FXML来创建一个新的父级,然后将新场景设置到舞台上。需要注意的关键是,您创建了一个新的父级和场景,它不知道您在其他场景中为滑块设置的任何值

static void launchScreen(String fileName) {
    fileName = "/screens/" + fileName + ".fxml";
    try {
        Parent root =            
        FXMLLoader.load(MainMenuController.class.getResource(fileName));
        Scene scene = new Scene(root);
        stage.setTitle("Fortress Combat");
        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(t -> {
            Platform.exit();
            System.exit(420);
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}
一些建议:

保存实际场景,然后如果filename参数对应于已创建的场景,则可以真正切换场景,而不是创建新场景。 下面是一个快速制作的示例,其中包含一个硬编码的if语句,这只是为了向您展示我的意思,但我相信您可以用更少的重复代码更好地解决它

private Scene sceneA;

static void launchScreen(String fileName) {
    if(fileName.equals("sceneA") && sceneA != null){
         /*if we want to open up sceneA and it has been created before (meaning it's not null) then open the already existing scene.*/
            stage.setTitle("Fortress Combat");
            stage.setScene(sceneA);
            stage.show();
            stage.setOnCloseRequest(t -> {
                Platform.exit();
                System.exit(420);
            });

    }else{
        fileName = "/screens/" + fileName + ".fxml";
        try {
            Parent root =            
            FXMLLoader.load(MainMenuController.class.getResource(fileName));
            Scene scene = new Scene(root);
            stage.setTitle("Fortress Combat");
            stage.setScene(scene);
            stage.show();
            stage.setOnCloseRequest(t -> {
                Platform.exit();
                System.exit(420);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }   
}
建议2是将它们存储在某个地方,如果希望持久化,可以将它们存储在某个处理程序类或文件中。 或者建议3,可能是在控制器中声明一些静态变量,当滑块值发生更改时,您可以将这些变量设置为滑块的值。然后,您可以通过访问静态变量在初始化方法中设置滑块值。 不管怎样,这些都是我的想法,我希望我能有所帮助。
让我们知道它是如何工作的:

@kleopatra我需要添加什么?我认为我的问题对于这些属性是清楚的?您确实阅读了参考帮助页,不是吗;您的想法是您提供了一切,允许我将其放入IDE并重现您的问题……很高兴我能提供帮助!如果答案确实回答了您的问题,请接受答案r问题,并帮助您解决问题。
private Scene sceneA;

static void launchScreen(String fileName) {
    if(fileName.equals("sceneA") && sceneA != null){
         /*if we want to open up sceneA and it has been created before (meaning it's not null) then open the already existing scene.*/
            stage.setTitle("Fortress Combat");
            stage.setScene(sceneA);
            stage.show();
            stage.setOnCloseRequest(t -> {
                Platform.exit();
                System.exit(420);
            });

    }else{
        fileName = "/screens/" + fileName + ".fxml";
        try {
            Parent root =            
            FXMLLoader.load(MainMenuController.class.getResource(fileName));
            Scene scene = new Scene(root);
            stage.setTitle("Fortress Combat");
            stage.setScene(scene);
            stage.show();
            stage.setOnCloseRequest(t -> {
                Platform.exit();
                System.exit(420);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }   
}