사용자가 파일 메뉴 저장 명령을 클릭하면 모든 객체를 직렬화하려고합니다. 정확하게 이해하고 있다면 defaultReadObject()
과 defaultWriteObject()
은 정적 멤버를 제외한 모든 개체를 저장하므로이를 사용하려고했습니다. 내 프로그램은 일종의 그래프 그리기 프로그램입니다. 사용자가 알고리즘을 실행할 수있는 컨트롤 프레임과 그래프가 그려지는 다른 프레임이 있습니다. 사용자는 다음 및 이전 버튼을 클릭하여 알고리즘이 진행되는 노드를 확인할 수 있습니다. 그래서 사용자가 종료 후 스테핑을 계속할 수 있도록 도면의 실제 상태를 저장하고 싶습니다. defaultWriteObject
이 writeObject
메서드에서 호출되어야한다고 읽었지만 내 문제는 주 기능에서 생성되는 actionlistener에서 Control 및 Draw2 클래스의 인스턴스를 어떻게 저장할 수 있습니까? 내 코드의 관련 부분 :actionlistener에서 serialize
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/)를 게시 할 때 다음 제어를 전달합니다. 또한 일반적으로 응용 프로그램 당 하나의 프레임 만 만들어야합니다. 나머지는 대개 대화 상자 또는 옵션 창이 있습니다. –