在 JavaFX GUI 上保存用户创建的节点
saving nodes created by user on JavaFX GUI
在此处输入图像描述我在 JavaFX 上制作了一个用于创建时间 tables 的 GUI。当我打开应用程序时,我可以将计划(按钮)添加到日列(VBox)。 ofc 关闭应用程序后未保存更改:下次打开它时 table 为空。
我的问题是如何让它保存用户创建的节点,以便下次我打开应用程序时它们就在那里?
这是添加节点的确切部分,以达到其价值:
void ask_add_plan(ActionEvent event)
{
Button b = (Button) event.getSource();
VBox v = (VBox) b.getParent();
AnchorPane pop_up = new AnchorPane(); //this pop up is to specify things about the plan
//but i removed unnecessary code for simplicity
VBox pop_up_v = new VBox();
Button add = new Button("Add");
add.setOnAction(e->{
Button plan = new Button(what_to_do.getText());
v.getChildren().add(plan);
container.getChildren().remove(pop_up); //container is the anchor pane everything's in
});
pop_up_v.getChildren().add(add);
pop_up.getChildren().add(pop_up_v);
container.getChildren().add(pop_up); //container is the anchor pane everything's in
}
您应该使用 MVP(模型-视图-演示者)模式。在 UI 层中保存数据不是一个好的做法。使用数据创建模型,然后将其序列化。
JavaFX 节点只是您数据的表示。他们不应该被拯救。将实际数据本身存储在 class.
的私有字段中
- 在您的 Application.stop 方法中,将数据写入文件。
- 在您的 Application.start 方法中,读取该文件并使用它重建 JavaFX 节点。
private static final Path PLANS_FILE =
Path.of(System.getProperty("user.home"), "plans.txt");
private final List<String> plans = new ArrayList<>();
void ask_add_plan(ActionEvent event) {
// ...
add.setOnAction(e -> {
String planText = what_to_do.getText();
plans.add(planText);
Button plan = new Button(planText);
v.getChildren().add(plan);
container.getChildren().remove(pop_up);
});
}
@Override
public void start(Stage primaryStage)
throws Exception {
// ...
if (Files.exists(PLANS_FILE)) {
plans.addAll(Files.readAllLines(PLANS_FILE));
// Add UI elements for each stored plan.
for (String planText : plans) {
Button planButton = new Button(planText);
v.getChildren().add(planButton);
container.getChildren().remove(pop_up);
}
}
}
@Override
public void stop()
throws IOException {
Files.write(PLANS_FILE, plans);
}
以上只是一个简化的例子。该文件不必只存储字符串。
我的印象是在应用程序中创建的数据比计划字符串更复杂。对于复杂的数据,XML 可能是更合适的文件格式。或者您可以使用 Java 序列化。或者,您可以发明自己的文件格式。
您也不必读取和写入文件。如果您熟悉数据库编程,则可以使用数据库。或者您可以使用 Preferences(尽管首选项不太适合存储复杂数据列表)。
在此处输入图像描述我在 JavaFX 上制作了一个用于创建时间 tables 的 GUI。当我打开应用程序时,我可以将计划(按钮)添加到日列(VBox)。 ofc 关闭应用程序后未保存更改:下次打开它时 table 为空。 我的问题是如何让它保存用户创建的节点,以便下次我打开应用程序时它们就在那里?
这是添加节点的确切部分,以达到其价值:
void ask_add_plan(ActionEvent event)
{
Button b = (Button) event.getSource();
VBox v = (VBox) b.getParent();
AnchorPane pop_up = new AnchorPane(); //this pop up is to specify things about the plan
//but i removed unnecessary code for simplicity
VBox pop_up_v = new VBox();
Button add = new Button("Add");
add.setOnAction(e->{
Button plan = new Button(what_to_do.getText());
v.getChildren().add(plan);
container.getChildren().remove(pop_up); //container is the anchor pane everything's in
});
pop_up_v.getChildren().add(add);
pop_up.getChildren().add(pop_up_v);
container.getChildren().add(pop_up); //container is the anchor pane everything's in
}
您应该使用 MVP(模型-视图-演示者)模式。在 UI 层中保存数据不是一个好的做法。使用数据创建模型,然后将其序列化。
JavaFX 节点只是您数据的表示。他们不应该被拯救。将实际数据本身存储在 class.
的私有字段中- 在您的 Application.stop 方法中,将数据写入文件。
- 在您的 Application.start 方法中,读取该文件并使用它重建 JavaFX 节点。
private static final Path PLANS_FILE =
Path.of(System.getProperty("user.home"), "plans.txt");
private final List<String> plans = new ArrayList<>();
void ask_add_plan(ActionEvent event) {
// ...
add.setOnAction(e -> {
String planText = what_to_do.getText();
plans.add(planText);
Button plan = new Button(planText);
v.getChildren().add(plan);
container.getChildren().remove(pop_up);
});
}
@Override
public void start(Stage primaryStage)
throws Exception {
// ...
if (Files.exists(PLANS_FILE)) {
plans.addAll(Files.readAllLines(PLANS_FILE));
// Add UI elements for each stored plan.
for (String planText : plans) {
Button planButton = new Button(planText);
v.getChildren().add(planButton);
container.getChildren().remove(pop_up);
}
}
}
@Override
public void stop()
throws IOException {
Files.write(PLANS_FILE, plans);
}
以上只是一个简化的例子。该文件不必只存储字符串。
我的印象是在应用程序中创建的数据比计划字符串更复杂。对于复杂的数据,XML 可能是更合适的文件格式。或者您可以使用 Java 序列化。或者,您可以发明自己的文件格式。
您也不必读取和写入文件。如果您熟悉数据库编程,则可以使用数据库。或者您可以使用 Preferences(尽管首选项不太适合存储复杂数据列表)。