我将如何save/load这个JButtons的配置?

How would I save/load this configuration of JButtons?

这就是我将所有 JButton 添加到面板和数组列表的方式

  private ArrayList<JButton> b;
  String defaultLogo = "O";


  for(int i=0; i<81;i++)
  {

      b.add(new JButton(defaultLogo));
      b.get(i).addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < b.size(); i++){
                if (e.getSource() == b.get(i)){
                    b.get(i).setText(getSymbol());
                    b.get(i).setForeground(getColor());
                    b.get(i).setBackground(getBackColor());
                }
            }

        }

      });
      tilePanel.add(b.get(i));

  }

该程序允许用户选择一个符号、背景色和前景色,当按下每个 JButton 时,它会更改为选定的符号、前景色和背景色。

我希望能够使用 DataOutputStream 和 DataInputStream 保存 JButton 配置。我有两个动作侦听器附加到保存和加载按钮,按下时激活保存和加载方法。我应该在每个方法中编写什么以允许用户保存和加载 JButton 配置文件。

save = new JMenuItem("Save");
  file.add(save);
  save.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==save){
            save();
        }

    }

  });

  load = new JMenuItem("Load");
  file.add(load);
  load.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == load){
            load();
        }

    }

    });

here's an image of the program when its running

And another one

您可以尝试序列化,保存一个包含按钮的对象。

这是一个例子:

保存数据的对象

import java.io.Serializable;
import java.util.ArrayList;

public class Config implements Serializable {
    private static final long serialVersionUID = 1L;
    private ArrayList<String[]> Data;
    public Config(){
        Data=new ArrayList<String>();

    }

    public void addData(String path){
        Data.add(path);
    }
    public String getData(int index){
        return Data.get(index);
    }

}

连载:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Paths;
import java.io.File;
public class FileSerial {

    private FileSerial(){}

    public static Config deserialize(){

        Config result;

        try {
            FileInputStream fis = new FileInputStream(Paths.get("path to your file"));
            ObjectInputStream ois = new ObjectInputStream(fis);
            result = (Config) ois.readObject();
            ois.close();
        } catch (Exception e){
            e.printStackTrace();
    }

        return result;

    }

    public static void serialize(Config obj){
        try {

            File file = new File(Paths.get("path to file"));
            if(!file.exists()){
                file.getParentFile().mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(path.toString());
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            oos.close();

        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

要序列化的对象的文件名应具有扩展名“.ser”