Java .txt文件中的对象不会写入我的Jlist

Java .txt文件中的对象不会写入我的Jlist,java,file-io,jlist,Java,File Io,Jlist,我的Java应用程序有点问题。我有两个框架,主机称为MasterFrame。主框架包含一个JList+3个按钮 按钮1可以将块形状添加到JList,这将调用称为块帧的第二帧。块形状是存储在ArrayList shapecollection中的对象 主框架还包含一个保存按钮,该按钮将对象存储在名为“test.txt”的.txt文件中 主框架还包含一个加载按钮,该按钮将打开.txt文件“test.txt”,读取对象文件并将对象设置回JList。这才是真正的问题所在。save函数可以工作,我不太确定加

我的Java应用程序有点问题。我有两个框架,主机称为MasterFrame。主框架包含一个JList+3个按钮

按钮1可以将块形状添加到JList,这将调用称为块帧的第二帧。块形状是存储在ArrayList shapecollection中的对象

主框架还包含一个保存按钮,该按钮将对象存储在名为“test.txt”的.txt文件中

主框架还包含一个加载按钮,该按钮将打开.txt文件“test.txt”,读取对象文件并将对象设置回JList。这才是真正的问题所在。save函数可以工作,我不太确定加载方法。看起来它实际上是在读取我的.txt文件中的对象,但它不会把它放回我的Jlist中

load方法可能不错,但读取对象并将其设置回JList可能会出现问题。我很高兴你们能帮我一把:)

import java.io.*;
//我的文件IO类
公共类形状控制器{
私有字符串文件名;
公共形状控制器(){
filename=“C:\\Users\\Lars\\Desktop\\test.txt”;
}
//Save方法,将我创建的所有块保存在“test.txt”文件中
公共作废保存(ShapeCollection ShapeCollection){
试一试{
ObjectOutputStream out=新的ObjectOutputStream(新文件输出流(文件名));
out.writeObject(shapecollection);
out.close();
}
捕获(IOE异常){
System.out.println(“尝试打开文件时出错”+文件名);
e、 printStackTrace();
}
}
//打开文件方法,该方法将打开“test.txt”文件,
//读取它的对象并返回形状的数组列表“shapecollection”
公共形状集合打开(形状集合形状集合){
试一试{
ObjectInputStream in=新ObjectInputStream(新文件输入流(文件名));
.readObject()中的shapecollection=(shapecollection);
in.close();
}
catch(classnotfounde异常){
System.out.println(“读取文件的未知类”+文件名);
}
捕获(IOE异常){
System.out.println(“尝试打开文件时出错”+文件名);
e、 printStackTrace();
}
shapecollection.giveCollection();
返回形状集合;
}
}
//“保存”按钮的工作代码
私有void SaveShapeButtonActionPerformed(java.awt.event.ActionEvent evt){
save(shapecontroller.getShapeCollection());
} 
//可能出错的代码
私有void LoadShapeButtonActionPerformed(java.awt.event.ActionEvent evt){
对于(int i=0;i
您尝试过调试吗?你到底在哪里面对这个问题?是的,我试过调试,它没有显示出任何问题。我认为问题出在这里,当您按下“加载”按钮时,向JList显示读取的对象。私有void LoadShapeButtonActionPerformed方法。问题是,当我添加块形状并将其保存到“test.txt”文件时,“加载”按钮起作用,但当我关闭程序并再次启动程序并按下“加载”时,它不会显示test.txt文件中的对象。
import java.io.*;

// My File IO Class
public class ShapeIOController {
private String filename;


public ShapeIOController(){
filename= "C:\\Users\\Lars\\Desktop\\test.txt";
}

// The Save method which will save all my created blocks in "test.txt" file
public void save(ShapeCollection shapecollection){

    try {
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename)); 
    out.writeObject(shapecollection);
    out.close();
    }
    catch( IOException e){
    System.out.println("Error occured when trying to open file" + filename);
    e.printStackTrace();
    }
}

// The open file method which will open "test.txt" file, 
// read it's objects and return a ArrayList of shapes "shapecollection"
public ShapeCollection open(ShapeCollection shapecollection){
     try{
     ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
     shapecollection= (ShapeCollection) in.readObject();
     in.close();
     }
     catch (ClassNotFoundException e ) {
     System.out.println("Unknown class by reading file" + filename);
     }
     catch ( IOException e ) {
     System.out.println("Error occured when trying to open file " + filename);
     e.printStackTrace();
     }

shapecollection.giveCollection();
return shapecollection;
    }
}

// The code for the save button which works
 private void SaveShapeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    shapeiocontroller.save(shapecontroller.getShapeCollection());
} 

// The code where it probably is going wrong
 private void LoadShapeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    for(int i = 0; i < shapecontroller.getShapeCollection().giveCollection().size(); i++){
    listModel.addElement(shapecontroller.getShapeCollection().giveShape(i).toString());
    InfoShapeJList.setModel(listModel);
    shapeiocontroller.open(shapecontroller.getShapeCollection());
    }   
}

// List of methods LoadShapeButtonActionPerformed is using:

// getShapeCollection()
public ShapeCollection getShapeCollection() {
    return shapecollection;
}

// giveCollection()
public ArrayList<Shape> giveCollection(){
   return shapecollection;
}

// giveShape()
 public Shape giveShape(int index){
    return shapecollection.get(index);     
 }

// toString()
 public String toString(){
    return "Block: " + length + " " + width + " " + height;
}