Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用文件/对象输入流加载游戏_Java_Serialization_Load_Deserialization_Objectinputstream - Fatal编程技术网

Java 如何使用文件/对象输入流加载游戏

Java 如何使用文件/对象输入流加载游戏,java,serialization,load,deserialization,objectinputstream,Java,Serialization,Load,Deserialization,Objectinputstream,我编写了保存/加载方法,不完全确定保存是否有效,但调用saveGame后,我的项目文件夹中会出现文件“mineswipersavestate.ser”。我想试着让负载工作,因为目前它似乎没有任何作用 以下是我的职能: public void saveGame(){ GameBoard b = new GameBoard(); try { System.out.println("Creating File/Object output

我编写了保存/加载方法,不完全确定保存是否有效,但调用saveGame后,我的项目文件夹中会出现文件“mineswipersavestate.ser”。我想试着让负载工作,因为目前它似乎没有任何作用

以下是我的职能:

   public void saveGame(){

        GameBoard b = new GameBoard();
        try {
            System.out.println("Creating File/Object output stream...");
            FileOutputStream fileOut = new FileOutputStream("minesweepersavestate.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            System.out.println("Writing GameBoard Object...");
            out.writeObject(b);

            System.out.println("Save Successful...\n");
            out.close();
            fileOut.close();

        } catch(FileNotFoundException e) {
            System.out.println("no");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("no");
            e.printStackTrace();
        } 

    }


    public void loadBoard()
    {
        GameBoard b = null;
        try {

            System.out.println("Creating File/Object input stream...");

            FileInputStream fileIn = new FileInputStream("minesweepersavestate.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);

            System.out.println("Loading GameBoard Object...");
            b = (GameBoard)in.readObject();

            System.out.println("Load Successful...\n");
            in.close();
            fileIn.close();

        } catch (ClassNotFoundException e) {
            System.out.println("Class not found\n");
            e.printStackTrace();
        } catch(FileNotFoundException e) {
            System.out.println("File not found\n");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
如何更改此方法,以便在调用loadBoard方法时,它将游戏恢复到保存状态

编辑:

游戏板类:

public class GameBoard extends JFrame implements Serializable
{
//MENU ITEMS
private JFrame frame;
private JMenuBar menubar = new JMenuBar();
private JTable table;

//FIELDS USED FOR GAMEPLAY
private int x;
private int y;
private boolean mineTrue;
private boolean triedTrue;
private static int boardsize = 8; 
private  int numberOfMines = 10;
public static final int Empty = 0;
public static final int Mine = -1;
public static final int Flag = -2;
public static final int FMine = -3;
public static final int RevealedMine = -4;
public static final int RevealedEmpty = -5;
// SIZE OF BUTTONS
private int gridsize = 45;
// ARRAY FOR THE BUTTONS
private JButton[][] buttons;
private int[][] board;
//VARIABLE USED FOR LABELS
private static int noGamesPlayed = 0;
private static int noGamesWon = 0;
private int mine = 10;
private int minesLeft = 10;
private static int score = 1;
private static String playername;
// GAME STATUS
private boolean gamegoing = true;
// GAME LABELS

private JLabel space = new JLabel("");
private JTextField nameEnter = new JTextField("Enter name here: ");
private JButton saveName = new JButton("Play");
private JLabel namelabel = new JLabel("Player 1: ");
private JLabel scorelabel = new JLabel("0 points ");
private JLabel status = new JLabel("Game in Progress: ");
private JLabel gamesPlayed = new JLabel("Games Played: " + noGamesPlayed);
private JLabel gamesWon = new JLabel("Games Won : " + noGamesWon);
private JLabel noMines = new JLabel("Number of Mines: " + minesLeft);


/**
 * Constructor 
 */
public GameBoard() { }

/**
 *Making the game Frame
 */
private void makeFrame() { }

// MAKING THE GAME MENU BAR
public void makeMenuBar(){  
}   

public void setx(int pmeter) { }

public void sety(int pmeter) { }

public int getx() { }

public int gety() { }

public void settried(boolean paramBoolean) { }

public boolean gettried() { }

public void setmine(boolean paramBoolean) { }

public boolean getmine() { }

//ASSIGN MINES TO RANDOM LOCATION
public void assignmines() { }

// *********************************GAME CONTROLS************
private void quit() { }

//RESETS THE BOARD
public void reset() { }    

public void newGame() { }

public void biggerBoard() { }

public void changeDifficulty() { }

public void saveGame() { }


public void loadBoard() { }

// LOSING THE GAME ALERT
public void lose() { }

// WINNING THE GAME ALERT
public void win() { }

// UPDATING THE SCORE
public void updatescore() { }

public void gamesPlayed() { }

public void UpdateName() { }

public void updateGamesPlayed() { }

public void updateGamesWon() { }

//WHAT VALUES THE CHARACTER HAVE
static char whichCharacter(int value) { }

//This method shows how many mines are around the spot that was clicked
static int minesAround(int[][] board, int row, int col) { }

// This method takes in an x and y value and defines what should happen when the user clicks there. 
public void click(int rows, int cols) { }

//ACTION WHEN USER CLICKS ON A BUTTON INNER CLASS
private class MouseListener extends MouseAdapter {
    private int x = 0;
    private int y = 0;

    public MouseListener(int row, int col) { }

    public void mouseClicked(MouseEvent e) { }

}

}

您需要确保类中的所有字段都可以按照建议进行序列化。需要特别注意的是类可序列化的要求:

请注意,要成功序列化类,必须满足两个条件:

该类必须实现java.io.Serializable接口

类中的所有字段都必须是可序列化的。如果字段不可序列化,则必须将其标记为瞬态

这意味着您必须对GameBoard类中的所有字段在Javadoc中进行一些挖掘。例如,我在JFrame上做了一个快速搜索,当涉及到 保存JFrame。当您检索已保存游戏的状态时,最好从头开始重新创建GUI,而不是依赖序列化来为您完成。似乎也同意依靠Java为您恢复GUI不是一个好主意,但给出了如何使其工作的建议:

反序列化后,需要使用frame.setVisibletrue显示帧


加载时是否出现异常?或者b只是空的?你能为你的GameBoard对象提供一个代码大纲吗?类声明、字段和方法签名?已编辑。实际上,我刚刚检查了一下,现在保存不起作用,我刚刚得到一个错误:GameBoard.clickGameBoard.java:434上线程AWT-EventQueue-0 java.lang.NullPointerException中的异常