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

问题描述

我想为我的游戏创建一个更新/刷新方法,该方法将读取拼图和堆栈对象上的数据,并更新 GUI 以匹配从上次保存加载游戏时的状态。我创建了一个可以正常工作的保存和加载方法,但是我不确定如何使用更新/刷新方法让输入显示在 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,row,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+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.",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.",JOptionPane.DEFAULT_OPTION); 
    } catch (IOException e) {
    JOptionPane.showConfirmDialog(null,e.toString() + "\nFailed to load game.",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,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)){
            relCellsCol[col][row][col][row].setText("\u25B2");
            relCellsCol[col][row][col][row].setForeground(Color.DARK_GRAY);
          }
          else if(game.containsRelEntry(col,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("");
          }
        }
      }
    }     
  }
}