2

好的,这应该是我现在的最后一个问题 :) 无论如何,我有一个称为 mode 的字符串和一个称为行和组件的 ArrayList,它们在我的程序中通过各种函数进行编辑。一切正常。但是当我从一个序列化的保存文件中加载时,我无法从函数中编辑这些值。加载后,如果我尝试更改 mode 的值或添加到其中一个数组列表,则更改会在该函数内发生,但是当该函数结束时它会恢复为旧值。加载后如何从函数内全局更改这些字段?

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;
      }
    }
}
4

0 回答 0