好的,这应该是我现在的最后一个问题:)无论如何,我有一个名为mode的字符串和ArrayLists,称为行和组件,可以通过各种函数在我的程序中进行编辑。一切都很正常。但是当我从序列化保存文件加载时,我无法从函数内编辑这些值。加载后,如果我尝试更改模式的值或添加到其中一个arraylists,则更改发生在该函数中,但是当该函数结束时它会恢复为旧的值。如何在加载后从函数内全局更改这些字段?
public class Main implements Serializable {
ArrayList<Component> components = new ArrayList<Component>();
ArrayList<Connection> lines = new ArrayList<Connection>();
String mode = "";
void makeErase() {
mode = "erase";
header.setText("Click something to erase it");
}
abstract class Component extends JLabel implements MouseListener,
MouseMotionListener,
Serializable {
public void mousePressed(MouseEvent e) {
if (mode.equals("erase")) {
drawPanel.remove(this);
components.remove(this);
for (Input input : inputs) {
for (Connection connection : input.connections) {
System.out.println("hey");
drawPanel.remove(connection);
lines.remove(connection);
connection.output.connections.remove(connection);
}
}
}
void save() throws IOException {
String savepoint = "C:\\apcs\\hi.txt";
FileOutputStream fileOut = new FileOutputStream(savepoint);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
for (Component c : components) {
for (Output o : c.outputs) {
for (Input i : o.inputsReceivingThis) {
}
}
}
ProjectState state = new ProjectState();
out.writeObject(state);
out.close();
fileOut.close();
}
void load() throws IOException, ClassNotFoundException {
ProjectState state;
String loadpoint = "C:\\apcs\\hi.txt";
FileInputStream fileIn = new FileInputStream(loadpoint);
ObjectInputStream in = new ObjectInputStream(fileIn);
state = (ProjectState) in.readObject();
in.close();
fileIn.close();
drawPanel.removeAll();
drawPanel.repaint();
components.remove(components);
lines.remove(lines);
components=state.projectComponents;
for (Component component : state.projectComponents) {
component.addMouseListener(component);
component.addMouseMotionListener(component);
drawPanel.add(component);
for (Output output : component.outputs) {
for (Input input : output.inputsReceivingThis) {
Connection c = new Connection(currentConnectionID, output, input);
lines.add(c);
}
}
}
}
}
class ProjectState implements Serializable {
ArrayList<Component> projectComponents;
ArrayList<Connection> projectLines;
public ProjectState() {
projectComponents = components;
projectLines = lines;
}
}
}