如何创建一个 update/refresh 方法来读取新加载的对象实例并将其应用于 GUI?

How to create an update/refresh method that reads the newly loaded object instances and applies it to the GUI?

我想为我的游戏创建一个 update/refresh 方法,该方法将读取拼图和堆栈对象上的数据并更新 GUI 以匹配游戏从上次保存加载时的状态。我已经创建了一个工作正常的保存和加载方法,但是我不确定如何使用 update/refresh 方法让输入显示在 GUI 上。

public class PuzzleGUI extends JFrame implements Observer{

private Puzzle game; 
private JPanel mainGrid; 
private Stack<UserEntry> stack = null;

  private void init() { 
    game = new Puzzle(); 
    game.addObserver(this);
    stack = new Stack<UserEntry>();  

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(360, 600); 
    this.setResizable(false); 

    Container pane = this.getContentPane();
    pane.setLayout(new BorderLayout()); 

    mainGrid = new JPanel();
    mainGrid.setLayout(new FlowLayout());
    pane.add("Center", mainGrid);

    JButton load = new JButton("Load");
    mainGrid.add(load);           
    load.addActionListener(new ActionListener() 
    {
      @Override
      public void actionPerformed(ActionEvent ae){
        load();     
      }
    });

    setupGrid();
    setVisible(true); 
  }

  public void setupGrid() {
    cells = new PanelCell[5+1][5+1]; 
    relCellsRow = new RelCellRow[5+1][5+1][5+1][5+1];
    relCellsCol = new RelCellCol[5+1][5+1][5+1][5+1];
    greyBtns = new GreyButtons(); 
      
    for(int col=1; col<6; col++){
      JPanel panelx = new JPanel();   
      mainGrid.add(panelx);  
      JPanel cpanelx = new JPanel(); 
    for(int row=1; row<6; row++){ 
      cells[col][row]= new PanelCell(this,col,row);
      panelx.add(cells[col][row]);
        if(row<5){
              relCellsRow[col][row][col][row+1]= new RelCellRow(this,col,row,col,row+1);
              panelx.add(relCellsRow[col][row][col][row+1]);  
        }
        if(col<5){
          if(row<6){
              relCellsCol[col][row][col+1][row]= new RelCellCol(this,col,row,col+1,row);
              cpanelx.add(relCellsCol[col][row][col+1][row]);
              if(row<5){
                greyBtns= new GreyButtons();
                cpanelx.add(greyBtns); 
              }
          }
       }
    } 
       mainGrid.add(panelx);
       mainGrid.add(cpanelx);
    }
  }

private void save() {
    try {
    PrintStream ps = new PrintStream(new File(file));
    for (UserEntry ue : stack) {
        ps.println(ue.toStringForFile());
    }

    ps.close();
    JOptionPane.showConfirmDialog(null, 
        "Game saved successfully.", 
        "Puzzle",   
        JOptionPane.DEFAULT_OPTION);   
    } catch (IOException e) {
    JOptionPane.showConfirmDialog(null, 
        e.toString() + "\nFailed to save game.",   
        "Puzzle", 
        JOptionPane.DEFAULT_OPTION); 
    }    
}

private void load(){
    try {
    Scanner fscnr = new Scanner(new File(file));
    clear();
    while (fscnr.hasNextInt()) {
        UserEntry a = new Assign(fscnr);
        UserEntry re = new RelEntry(fscnr); 
        game.assign(a.getRow(),a.getCol(),a.getNum());
        game.addRelation(re.getGreaterRow(), re.getGreaterCol(), re.getLesserRow(), re.getLesserCol());   
        stack.push(a);
        stack.push(re); 
    }
    fscnr.close();
    JOptionPane.showConfirmDialog(null, 
        "Game loaded successfully.", 
        "Puzzle",   
        JOptionPane.DEFAULT_OPTION); 
    } catch (IOException e) {
    JOptionPane.showConfirmDialog(null, 
        e.toString() + "\nFailed to load game.",   
        "Puzzle", 
        JOptionPane.DEFAULT_OPTION); 
    } 
}

通过创建一个名为 reloadGame 的新方法找到解决方案,该方法检查游戏中是否分配了任何输入。然后在 load 方法中声明。

private void reloadGame(){  
  game.addObserver(this); 
    for(int col=1; col<6; col++){
      for(int row=1; row<6; row++){ 
        if(game.isAssigned(col,row)){
          cells[col][row].setText(Integer.toString(game.getNum(col,row)));             
        }else{
          cells[col][row].setText("");
        }
        if(row<5){
          if (game.containsRelEntry(col,row,col,row+1)){
            relCellsRow[col][row][col][row].setText("\u25B6");
            relCellsRow[col][row][col][row].setForeground(Color.DARK_GRAY);
        }else if(game.containsRelEntry(col,row+1,col,row)){
            relCellsRow[col][row][col][row].setText("\u25C0"); 
            relCellsRow[col][row][col][row].setForeground(Color.DARK_GRAY); 
        }else{
            relCellsRow[col][row][col][row].setText("");
        }
      }
      if(col<5){
        if(row<6){
          if (game.containsRelEntry(col+1,row,col,row)){
            relCellsCol[col][row][col][row].setText("\u25B2");
            relCellsCol[col][row][col][row].setForeground(Color.DARK_GRAY);
          }
          else if(game.containsRelEntry(col,row,col+1,row)){
            relCellsCol[col][row][col][row].setText("\u25BC"); 
            relCellsCol[col][row][col][row].setForeground(Color.DARK_GRAY);
          }else{
            relCellsCol[col][row][col][row].setText("");
          }
        }
      }
    }     
  }
}