gpt4 book ai didi

java - 如何将 ListView 保存为文本文档而不是将其加载回程序中

转载 作者:行者123 更新时间:2023-12-02 10:58:34 26 4
gpt4 key购买 nike

我目前正在制作一个程序,您可以在其中使用Java从 ListView 中添加和删除项目,我希望它能够在您向 ListView 添加项目和删除项目时自动保存。我很难弄清楚如何做到这一点,任何帮助将不胜感激。我对编程还很陌生,并且仍在尝试弄清楚这一切,这是我到目前为止的代码。

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javafx.application.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;



public class LendingLibraryGUI extends Application {
LendingLibrary LendingLibrary = new LendingLibrary(); //Creating an Object to access total numbers of items
MediaItems Media = new MediaItems(); // creating an array of object to access MediaItems class and allowing it to hold 100 items
private ListView<String> library = new ListView<String>();
ObservableList<String> libraryList = FXCollections.<String>observableArrayList("yes","no");

@Override
public void start(Stage primaryStage) throws Exception {
BorderPane display = new BorderPane(); //Main display
GridPane buttons = new GridPane(); //location to display buttons
TextField outPut = new TextField(); //Text field to show inventory
Insets padding = new Insets(10); //creates Insets for padding
buttons.setPadding(padding); //padding around grid pane
buttons.setHgap(10); //Horizontal gap
library.setItems(libraryList);



for (int i =0; i !=4;i++) { //Loop to create Buttons
String[] actionButtons = {"Add","Check Out","Check In","Delete"};//String to store Button names
Button temp = new Button(actionButtons[i]);
temp.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
buttons.add(temp, i, 0); //add buttons to grid pane
GridPane.setHgrow(temp, Priority.ALWAYS);
GridPane.setVgrow(temp, Priority.ALWAYS);
if (temp.getText().equals("Add")) {
temp.setOnAction((e) -> add());
}
else if (temp.getText().equals("Delete")) {

temp.setOnAction((e) -> deleteLibrary());

}
}


outPut.setEditable(false); //no editing
outPut.setFont(Font.font("monospace", FontWeight.BOLD, 20));
outPut.setMinHeight(300);//sets minimum height
display.setTop(library); //sets output in display on top
display.setCenter(buttons); //sets buttons on center


Scene scene = new Scene(display); //creates new scene
primaryStage.setTitle("Lending Library"); //sets title of GUI
primaryStage.setScene(scene); //adds scene to GUI
primaryStage.setMinHeight(400); //Minimum height
primaryStage.setMinWidth(350);//Minimum Width
primaryStage.show();//Displays GUI to user
}

public static void main(String[] args) {

launch(args);

}


private void add() {
inputGUI("Title:");
}

private void inputGUI(String input) {
Stage secondaryStage = new Stage();
BorderPane border = new BorderPane();
VBox titlePane = new VBox(8);
HBox buttonLayout = new HBox(8);
Label lblTitle = new Label(input);
Button save = new Button("Save");
Button close = new Button("Close");
Insets padding = new Insets(10);
TextField txt = new TextField("");
close.setOnAction((e) -> secondaryStage.close());;



save.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
try {

LendingLibrary.save(library);
} catch (IOException e1) {
e1.printStackTrace();
}

if (txt.getText().trim().isEmpty()) {

}
else {

if (input.equals("Title:")) {
Media.setTitle(txt.getText());
secondaryStage.close();
inputGUI("Format:");
}
else if (input.equals("Format:")) {
Media.setFormat(txt.getText());
secondaryStage.close();
addToLibrary();
}
else if (input.equals("Who did you loan this to?")) {



}
else if (input.equals("When did you loan it(date)?")) {


}
}

}
});

buttonLayout.getChildren().addAll(close,save);
titlePane.setPadding(padding);
titlePane.getChildren().addAll(lblTitle,txt,buttonLayout);
border.setCenter(titlePane);
BorderPane.setAlignment(titlePane, Pos.CENTER);

Scene scene = new Scene(border); //creates new scene
secondaryStage.setTitle("Input"); //sets title of GUI
secondaryStage.setScene(scene); //adds scene to GUI
secondaryStage.setMinHeight(200); //Minimum height
secondaryStage.setMinWidth(350);//Minimum Width
secondaryStage.show();//Displays GUI to user


}


private void addToLibrary() {
String total;
total = Media.getTitle();
total = total + " ("+ Media.getFormat() +")";
libraryList.add(total);
library.setItems(libraryList);



}

private void deleteLibrary() {
int selectedItem = library.getSelectionModel().getSelectedIndex();
libraryList.remove(selectedItem);
}

private void checkOut() {

}

}

任何其他指示或建议将不胜感激!

提前致谢

编辑:再说一次,我很新,只是想学习基本的东西,这不是我要继续阅读一本书的东西,这是它试图教我的东西。

public void save(ListView<String> library) throws IOException {
File file = new File ("LendingLibrary.txt"); //creates text file
PrintWriter output = new PrintWriter(file);

if(file.exists()) { //if the file exists
output.println(library);
output.close();

}

if(!file.exists()) { //if file doesn't exist
System.out.println("Error creating file");
}



}

最佳答案

您真正感兴趣的是保存 ListView 显示的数据,您不需要所有其他布局信息和内容,因为它们在应用程序中静态定义并在每次运行时自动加载。

现在,虽然将数据保存在文件中并在每次需要时加载它可以工作,但这通常不是最好的。更好的方法是使用数据库以关系实体的形式存储应用程序的数据,这样您就可以使用更安全、更一致的方法。要开始进入该主题,您可以继续咨询官方reference

如果您想首先尝试使用文件方法,建议是以某种结构化格式保存数据,这样可以轻松保存和加载,或者更准确地说序列化/反序列化。为此,您可以使用 json 格式将数据存储在文件中,并且可以使用 gson库例如:

  1. ListView 的每一行都是一个包含数据的对象。
  2. 阅读:使用 gson 将数据列表序列化为 json 格式,并将每个数据存储在单独的行中。
  3. 阅读:加载字符串列表并使用 gson 将它们反序列化为 java 类。

关于java - 如何将 ListView 保存为文本文档而不是将其加载回程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51525951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com