我有一堆在SceneBuilder中为Javafx构建的fxml窗口。当我在设置窗口和主菜单之间切换时,滑块(用于各种声音级别)会重置为其原始值,但是我希望它们保持上一次打开窗口时的值。
我尝试过初始化设置值和通过类设置变量,该类已更改并在启动时用于设置滑块,但这也不起作用。
<!-- The slider code (scenebuilder forces me to choose a value) ->
<Slider fx:id="soundfxSlider" onMouseDragged="#updateSFX" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="50.0" />
<Slider fx:id="musicSlider" minorTickCount="1" onMouseDragged="#updateMusic" showTickLabels="true" showTickMarks="true" snapToTicks="true" />
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();
}
}
public class SettingsController {
@FXML // fx:id="soundfxSlider"
private Slider soundfxSlider; // Value injected by FXMLLoader
@FXML // fx:id="musicSlider"
private Slider musicSlider; // Value injected by FXMLLoader
@FXML
void updateMusic(MouseEvent event) {
double musicVolume = musicSlider.getValue();
//setMusicSlider(musicVolume);
Launcher.adjustVolume(musicVolume);
}
@FXML
void updateSFX(MouseEvent event) {
double vol = soundfxSlider.getValue();
//setSoundfxSlider(vol);
Launcher.adjustSfx(vol);
}
private void setMusicSlider(double sliderVal) {
musicSlider.setValue(sliderVal);
}
private void setSoundfxSlider(double sfxVal) {
soundfxSlider.setValue(sfxVal);
}
@FXML
void playTestSfx(ActionEvent event) {
Launcher.playTestSFX();
}
@FXML
void goBack(ActionEvent event) {
Launcher.launchScreen("main_menu");
}
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert soundfxSlider != null : "fx:id=\"soundfxSlider\" was not injected: check your FXML file 'settings.fxml'.";
assert musicSlider != null : "fx:id=\"musicSlider\" was not injected: check your FXML file 'settings.fxml'.";
}
如果未将fxml中的滑块赋予任何值,则它们的默认值为0-似乎默认值是强制的,并且在切换窗口时不会“记住”。
答案 0 :(得分:1)
我相信我理解为什么您每次打开 new 窗口或加载 new 窗口时总是在滑块上获得默认值。
如果您看下面的代码,我们会看到您是通过加载FXML来创建新的父对象的,该FXML具有作为参数指定的名称,然后将新的Scene设置到舞台上。需要注意的关键是,您创建一个新父级和场景,但不知道在其他场景中将滑块设置为任何值。
const Client = require('node-rest-client').Client;
const client = new Client();
client.get("https://myapi.com/data.xml", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
一些建议:
下面是一个快速编写的示例,该示例带有一个硬编码的if语句,目的只是为了向您展示我的意思,但是我敢肯定,您可以用更少的重复代码来解决这个问题。
Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1049:34)
at TLSSocket.emit (events.js:182:13)
at TLSSocket._finishInit (_tls_wrap.js:631:8)
无论如何,这些是我的想法,希望我能有所帮助。 让我们知道它是如何工作的:)