gpt4 book ai didi

java - java中的序列化,在事件中重新膨胀对象?

转载 作者:行者123 更新时间:2023-12-04 06:05:50 26 4
gpt4 key购买 nike

我正在尝试更多地了解java。该程序试图理解事件和序列化。我试图做的是在用户关闭 JFrame 时展平一个对象,并在程序启动时重新充气它。我知道我可以创建序列化文件,但让它再次生效不起作用。任何在正确方向上的帮助都会很棒。先感谢您。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
String outT = Integer.toString(ToL);
JLabel jl = new JLabel(outT);

FileOutputStream fos = null;
ObjectOutputStream out = null;

public tempusFugit()

{
Timer timer = new Timer(1000, this);

setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}

public void actionPerformed(ActionEvent e)
{
++ToL;
outT = Integer.toString(ToL);
jl.setText(outT);
validate();
repaint();
}
public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );

}
public void windowDeactivated(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
try
{
fos = new FileOutputStream(fileN);
out = new ObjectOutputStream(fos);
out.writeObject(this);
out.close();

}
catch(IOException ex)
{
}
}
public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
}
catch(IOException ex)
{
}
catch(ClassNotFoundException ce)
{
}

}

}

我假设我试图在错误的时间重新创建对象。即使对象被正确序列化,我也无法使用 windowOpened 函数再次访问它。我需要尝试使用吗
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

不知何故?

我最终得到的是一个错误,说我正在尝试访问一个 Final 对象(我假设我的这个)。我觉得很奇怪,我不能用另一个类似的对象重新填充我当前的“this”。

我离基地很远吗?

再次感谢您的时间。

最佳答案

请注意,有比序列化框架更简单的方法来做到这一点,并且序列化比框架本身更好。

What is the best practice for setting JFrame locations in Java?例如存储帧的位置和大小。调整它来存储计数将是微不足道的。

但这是基于您的代码的尝试。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.Timer;
import java.io.*;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class tempusFugit extends JFrame implements ActionListener, Serializable, WindowListener
{
String fileN = "tf.txt";
public int ToL = 0;
JLabel jl = new JLabel("" + ToL);

public tempusFugit()
{
Timer timer = new Timer(1000, this);

setBounds(250, 250, 250, 190);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT) );
setVisible(true);
add(jl);
timer.start();
}

public void actionPerformed(ActionEvent e)
{
++ToL;
jl.setText("" + ToL);
validate();
repaint();
}

public static void main(String[] args)
{
tempusFugit tf = new tempusFugit();
tf.addWindowListener( tf );
}

public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}

public void windowClosing(WindowEvent e)
{
try
{
FileOutputStream fos = new FileOutputStream(fileN);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(this);
out.flush();
out.close();
setVisible(false);
System.exit(0);
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, ex);
System.exit(1);
}
}

public void windowOpened(WindowEvent e)
{
try
{
tempusFugit tf;// = new tempusFugit();
FileInputStream fis = new FileInputStream(fileN);
ObjectInputStream in = new ObjectInputStream(fis);
tf = (tempusFugit)in.readObject();
this.ToL = tf.ToL;
//tf.setVisible(false);
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
}
}

关于java - java中的序列化,在事件中重新膨胀对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373714/

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