Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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
Java 如何将ListView保存为文本文档,而不是将其加载回程序_Java_Listview - Fatal编程技术网

Java 如何将ListView保存为文本文档,而不是将其加载回程序

Java 如何将ListView保存为文本文档,而不是将其加载回程序,java,listview,Java,Listview,我目前正在制作一个程序,您可以在Java中从列表视图添加和删除项目,我希望它能够在您向列表视图添加项目和删除项目时自动保存。我很难弄明白如何做到这一点,任何帮助都将不胜感激。我在编程方面还是个新手,我一直在努力弄清楚这是我到目前为止的代码 import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javafx.application.*; import javafx.collectio

我目前正在制作一个程序,您可以在Java中从列表视图添加和删除项目,我希望它能够在您向列表视图添加项目和删除项目时自动保存。我很难弄明白如何做到这一点,任何帮助都将不胜感激。我在编程方面还是个新手,我一直在努力弄清楚这是我到目前为止的代码

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() {

    }

}
导入java.io.File;
导入java.io.IOException;
导入java.io.PrintWriter;
导入javafx.application.*;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ListView;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.Priority;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.scene.text.FontWeight;
导入javafx.stage.stage;
公共类LendingLibraryGUI扩展应用程序{
LendingLibrary LendingLibrary=new LendingLibrary();//创建一个对象以访问项目总数
MediaItems Media=new MediaItems();//创建一个对象数组来访问MediaItems类并允许它容纳100个项目
私有ListView库=新建ListView();
ObservableList libraryList=FXCollections.observableArrayList(“是”、“否”);
@凌驾
public void start(Stage primaryStage)引发异常{
BorderPane display=新建BorderPane();//主显示
GridPane buttons=new GridPane();//显示按钮的位置
TextField outPut=new TextField();//显示库存的文本字段
插入填充=新插入(10);//为填充创建插入
buttons.setPadding(padding);//在网格窗格周围填充
按钮。设置间隙(10);//水平间隙
library.setItems(libraryList);
对于(int i=0;i!=4;i++){//循环创建按钮
String[]actionButtons={“添加”、“签出”、“签入”、“删除”};//用于存储按钮名称的字符串
按钮温度=新按钮(操作按钮[i]);
温度设置最大值(双倍最大值,双倍最大值);
添加(临时,i,0);//将按钮添加到网格窗格
设置行(临时、优先级、始终);
setVgrow(临时、优先级、始终);
if(temp.getText().equals(“Add”)){
温度设置动作((e)->add());
}
else if(temp.getText()等于(“删除”)){
临时设置操作((e)->deleteLibrary());
}
}
outPut.setEditable(false);//不编辑
outPut.setFont(Font.Font(“monospace”,fontwweight.BOLD,20));
outPut.setMinHeight(300);//设置最小高度
display.setTop(库);//在顶部显示中设置输出
display.setCenter(按钮);//在中心设置按钮
场景=新场景(显示);//创建新场景
setTitle(“借出库”);//设置GUI的标题
primaryStage.setScene(场景);//将场景添加到GUI
primaryStage.setMinHeight(400);//最小高度
primaryStage.setMinWidth(350);//最小宽度
primaryStage.show();//向用户显示GUI
}
公共静态void main(字符串[]args){
发射(args);
}
私有void add(){
输入图形用户界面(“标题:”);
}
私有void输入GUI(字符串输入){
第二阶段阶段=新阶段();
BorderPane border=新的BorderPane();
VBox-标题烷=新的VBox(8);
HBox按钮布局=新HBox(8);
标签lblTitle=新标签(输入);
按钮保存=新按钮(“保存”);
按钮关闭=新按钮(“关闭”);
插图填充=新插图(10);
TextField txt=新的TextField(“”);
close.setOnAction((e)->secondaryStage.close());;
save.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
试一试{
LendingLibrary.save(库);
}捕获(IOE1异常){
e1.printStackTrace();
}
if(txt.getText().trim().isEmpty()){
}
否则{
if(input.equals(“Title:”)){
setTitle(txt.getText());
第二阶段。关闭();
inputGUI(“格式:”);
}
else if(input.equals(“格式:”){
setFormat(txt.getText());
第二阶段。关闭();
addToLibrary();
}
else if(input.equals(“你把这个借给谁了?”){
}
else if(input.equals(“你什么时候借的(日期)?”){
}
}
}
});
buttonLayout.getChildren().addAll(关闭,保存);
titlePane.设置填充(填充);
titlePane.getChildren().addAll(lbltite、txt、buttonLayout);
边界。设置中心(titlePane);
边界窗格。设置对齐(标题窗格,位置中心);
场景=新场景(边框);//创建新场景
secondaryStage.setTitle(“输入”);//设置GUI的标题
secondaryStage.setScene(场景);//将场景添加到GUI
第二阶段。设置最小高度(200);//最小高度
secondaryStage.setMinWidth(350);//最小宽度
secondaryStage.show();//向用户显示GUI
}
私有void addToLibrary(){
字符串总数;
total=Media.getTitle();
总计=总计+”(“+Media.getFormat()+”);
图书馆列表
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");
    }



}