Java - Scene Builder - TextField 和 ComboBox 中的用户输入在切换场景后消失
Java - Scene Builder - User input in TextField and ComboBox disappears after switching a scenes
我的Java项目中有几个场景和一个我真的不知道如何解决的问题。 First scene called "Zadanie". There are TextFields and ComboBoxes in this first scene called "Zadanie". So, as you can see in image, I wrote some numbers in TextFields and choosed some options in ComboBoxes. Then I switched on other scene by clicking on "Vypočítať" (button up). And then I switched back on first scene "Zadanie", but everything in TextFields and ComboBoxes is gone. Back on first scene "Zadanie". 请给我一些代码示例或如何将它们保留在第一个场景中的方法。谢谢。
问题是当您切换屏幕时,代表屏幕的对象被处理掉了。因此,任何变量(例如用户输入)也将被处理掉。当您切换回屏幕时,它是不同的对象但属于同一类型。这意味着用户输入等变量的值是新鲜的 instantiated/initialized.
解决方法:
创建一些存储此信息的全局变量(例如,Main 中的变量,但为了编程习惯,您应该使用不同的 类)。当用户点击切换屏幕时,但在屏幕实际切换(卸载)之前,将任何用户输入存储在全局变量中。然后当屏幕切换回时,将这些变量从全局变量重新加载到字段中。
Global.java
class Global {
public static String nameFieldValue;
public static String ageFieldValue;
}
PersonForm.java
class PersonForm extends BorderPane {
Button closeButton;
TextField nameField;
TextField ageField;
public PersonForm() {
this.nameField = new TextField(...);
this.ageField = new TextField(...);
this.closeButton = new Button("Close Window");
// set layouts of buttons here.
if (Global.nameFieldValue != null) {
this.nameField.setText(Global.nameFieldValue);
}
if (Global.passwordFieldValue != null) {
this.passwordField.setText(Global.passwordFieldValue);
}
PersonForm thisForm = this; // So that EventHandler cna use this.
this.closeButton.setOnAction(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Global.nameFieldValue = thisForm.nameField.getText();
Global.passwordFieldValue = thisForm.passwordField.getText();
// switch screens.
}
});
}
}
我的Java项目中有几个场景和一个我真的不知道如何解决的问题。 First scene called "Zadanie". There are TextFields and ComboBoxes in this first scene called "Zadanie". So, as you can see in image, I wrote some numbers in TextFields and choosed some options in ComboBoxes. Then I switched on other scene by clicking on "Vypočítať" (button up). And then I switched back on first scene "Zadanie", but everything in TextFields and ComboBoxes is gone. Back on first scene "Zadanie". 请给我一些代码示例或如何将它们保留在第一个场景中的方法。谢谢。
问题是当您切换屏幕时,代表屏幕的对象被处理掉了。因此,任何变量(例如用户输入)也将被处理掉。当您切换回屏幕时,它是不同的对象但属于同一类型。这意味着用户输入等变量的值是新鲜的 instantiated/initialized.
解决方法: 创建一些存储此信息的全局变量(例如,Main 中的变量,但为了编程习惯,您应该使用不同的 类)。当用户点击切换屏幕时,但在屏幕实际切换(卸载)之前,将任何用户输入存储在全局变量中。然后当屏幕切换回时,将这些变量从全局变量重新加载到字段中。
Global.java
class Global {
public static String nameFieldValue;
public static String ageFieldValue;
}
PersonForm.java
class PersonForm extends BorderPane {
Button closeButton;
TextField nameField;
TextField ageField;
public PersonForm() {
this.nameField = new TextField(...);
this.ageField = new TextField(...);
this.closeButton = new Button("Close Window");
// set layouts of buttons here.
if (Global.nameFieldValue != null) {
this.nameField.setText(Global.nameFieldValue);
}
if (Global.passwordFieldValue != null) {
this.passwordField.setText(Global.passwordFieldValue);
}
PersonForm thisForm = this; // So that EventHandler cna use this.
this.closeButton.setOnAction(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Global.nameFieldValue = thisForm.nameField.getText();
Global.passwordFieldValue = thisForm.passwordField.getText();
// switch screens.
}
});
}
}