Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Java_Save - Fatal编程技术网

如何在游戏关闭时保存java游戏?JAVA

如何在游戏关闭时保存java游戏?JAVA,java,save,Java,Save,我在将游戏数据保存到目录中时遇到了一些问题,我尝试了return和其他命令,但当我关闭游戏时。它不能保存。 我还尝试: public class FooState implements Serializable { Integer a, b, c; // Integer and String String d, e, f; // also implement Serializable } class Foo { static FooState allMyState =

我在将游戏数据保存到目录中时遇到了一些问题,我尝试了
return
和其他命令,但当我关闭游戏时。它不能保存。 我还尝试:

public class FooState implements Serializable {
    Integer a, b, c; // Integer and String
    String d, e, f;  // also implement Serializable
}

class Foo {
    static FooState allMyState = new FooState();

    public static void main(String[] args) throws IOException {
        try(ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(new File("gameStatus.data")))) {
            // no need to specify members individually
            oos.writeObject(allMyState);
        }
    }
}
但是没有任何效果,例如我在
JFrame
上有一个
JFrame
JLabel
按钮,我做了一个int,函数是当我点击按钮时int将增加(++)。当我关闭游戏时,我会保存我的游戏数据。它将重新启动。我如何保存它?请回答

一个例子:

import java.awt.BorderLayout;
import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class save extends JFrame {

        public static int a = 0;

        private JPanel contentPane;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        save frame = new save();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }


    /**
     * Create the frame.
     */
    public save() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 608, 402);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblPoints = new JLabel("Points: " + a);

        JButton btnHi = new JButton("hi");
        btnHi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                a++;
                lblPoints.setText("Points: " + a);
                save();
            }

            private int save() {
                // TODO Auto-generated method stub
                return a;
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                    .addContainerGap(146, Short.MAX_VALUE)
                    .addComponent(lblPoints, GroupLayout.PREFERRED_SIZE, 292, GroupLayout.PREFERRED_SIZE)
                    .addGap(144))
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(221)
                    .addComponent(btnHi, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(200, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGap(27)
                    .addComponent(lblPoints)
                    .addGap(230)
                    .addComponent(btnHi)
                    .addContainerGap(60, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }
}

我不知道如何保存这个。(当我点击按钮时,int a将增加[+])。

发布一个完整的、最小的示例,再现问题。准确地告诉我们你期望它做什么,以及它会做什么。你的问题非常令人困惑。好吧,我举了一个例子-Salmehb但你还没有解释代码应该做什么。你真的希望
返回一个
将游戏状态保存到文件中?你想保存什么?哪种格式?你试过做什么?它到底做了什么?我试图用C://文件保存游戏,格式为:save.dataSo,代码在哪里。它做什么而不是做你想做的?