尝试通过序列化保存我的Java程序时遇到了一个问题:加载后,按钮不再起作用。从调试我可以告诉我正在调用我的load方法并对数据进行反序列化。下面是我的Driver和FileIO课程,提前感谢任何提示。
`
public class Driver {
static FileIO go;
static Display ack;
public static int result;
public static void main(String[] args) {
JOptionPane loader = new JOptionPane();
result = loader.showConfirmDialog(null, "Would you like to load a character?");
if(result == 0){
go = new FileIO(true);
ack = go.load();
}
else if(result == 1){
go = new FileIO(false);
ack = new Display();
}
else{
System.exit(0);
}
Bar bar = new Bar();
ack.add(bar);
ack.setVisible(true);
}
}`
public class FileIO {
private String output;
private File file;
File written;
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output += output;
}
public FileIO(boolean isLoading) {
if(isLoading){
JTextArea ta = new JTextArea(20,30);
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(null);
if(status != JFileChooser.APPROVE_OPTION){
ta.setText("No File Chosen");
}
else{
file = chooser.getSelectedFile();
}
}
else{
try{
File dir = new File("C:\\RPG Toolbar");
dir.mkdir();
file = new File("C:\\RPG Toolbar\\Hero.ser");
if(file.createNewFile()){
System.out.println("New Hero Made");
}
else{
file = file.createTempFile("Hero", ".ser", dir);
System.out.println("New Hero Made (with random num)");
}
}
catch(Exception e){
System.out.println("Send halp!");
}
}
}
public void save(){
try{
// Serialize data object to a file
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(Driver.ack);
out.close();
// Serialize data object to a byte array
/*ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(Driver.ack);
out.close();*/
System.out.println("Saved!");
}
catch(Exception e){
System.out.println("Save failed!");
}
}
public Display load(){
Display x = null;
try{
FileInputStream door = new FileInputStream(file);
ObjectInputStream reader = new ObjectInputStream(door);
x = (Display) reader.readObject();
door.close();
reader.close();
System.out.println("Loaded!");
}
catch (Exception e){
}
return x;
}
}
请保持温和,我是Swing的新手,也是序列化的新手。