我試圖序列化所有我的對象時,用戶單擊文件菜單保存命令。 如果我理解正確,defaultReadObject()
和defaultWriteObject()
保存除靜態成員以外的所有對象,所以我嘗試使用這些對象。我的程序是一種圖形繪製程序。它有一個用戶可以設置算法運行的控制框架,以及一個用於繪製圖形的其他框架。用戶可以點擊下一個和上一個按鈕來查看算法正在經歷哪些節點。所以我想保存繪圖的實際狀態,以便用戶可以在退出後繼續步進。 我讀過defaultWriteObject
應該從writeObject
方法中調用,但我的問題是我不知道如何從actionlistener中保存Control和Draw2類的實例,這是在主函數中創建的? 我的代碼的相關部分:從actionlistener序列化
public class Control extends JPanel implements Serializable{
private String sourceFolder;
private String selectedAlg;
public List<Node> nodeList;
private JMenuBar jMenuBar;
private JMenu jMenu;
private JMenuItem jMenuItem;
private Draw2 draw;
private Search s;
//...
//and a lot more variables
void createControl(JFrame f, Draw2 d){
jMenuBar = new JMenuBar();
jMenu = new JMenu();
jMenuItem = new JMenuItem();
jMenu.setText("File");
jMenuBar.add(jMenu);
jMenuItem.setText("Save");
jMenuItem.addActionListener(new SaveAction());
jMenu.add(jMenuItem);
//building the control window with textfields, buttons..
draw=d;
s=new Search();
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
}
//inner class, actionlistener for save menuItem
public class SaveAction implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
try {
FileOutputStream f = new FileOutputStream("savedfile");
ObjectOutputStream out = new ObjectOutputStream(f);
//So how can save the instance of control and draw class
//here, which is created in the main function?
//how to call writeObject and with what parameter?
} catch (IOException o) {
}
}
}
public static void main(String[] args){
JFrame frame = new JFrame("Control");
JFrame frame2 = new JFrame("Draw");
Control cont = new control();
Draw2 gp = new Draw2(cont);
cont.createControl(frame, gp);
frame.setJMenuBar(cont.jMenuBar);
gp.setPreferredSize(new Dimension(0,0));
//Control Frame
frame.setSize(800,330);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(cont, BorderLayout.NORTH);
frame.setVisible(true);
//Drawing Frame
frame2.setSize(800,600);
frame2.setLocation(0, 330);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.add(gp);
frame2.setVisible(true);
}
}
public class Draw2 extends JPanel implements Serializable{
public boolean toDraw = false;
public boolean timedDraw = false;
public boolean steppedDraw = false;
private control c;
public Draw2(control co){
c=co;
}
public void paintComponent (Graphics g) {
//drawing the graph
}
}
而最後一個問題。當我從保存的文件中加載對象時,如何將運行對象替換爲加載的對象?我想通過一個參數來完成它,但actionPerformed除了ActionEvent之外不能有一個參數。
public class LoadAction implements ActionListener{
Control contr;
public LoadAction(Control control) {
contr = control;
}
public void actionPerformed(ActionEvent arg0) {
try {
FileInputStream f = new FileInputStream("savedfile");
ObjectInputStream in = new ObjectInputStream(f);
contr= (Control)in.readObject();
//so now I have the loaded object in the contr object,
//but How can copy it over the current object?
in.close();
System.out.println(contr.aktIndex);
} catch (IOException o) {
o.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。另外,通常每個應用程序只能創建1幀。其餘的通常是對話框或選項窗格。 –