我正在使用ObjectOutputStream保存一些对象。当我使用ObjectInputStream加载它们时,actionListeners无法正常工作。对象是JPanels,可以在容器中拖动。我怎样才能使它工作?没有太多可读的内容,所以我希望我能在这里得到一些信息。
private void loadInputStream (String file) {
ObjectInputStream ois = null;
try {
mainPanel.removeAll();
repaint();
FileInputStream fip = new FileInputStream(file);
ois = new ObjectInputStream(fip);
if (ois != null) {
while(fip.available() > 0) {
Component obj = (Component) ois.readObject();
mainPanel.add((Component) obj);
}
}
ois.close();
}catch (EOFException e) {
// Do Nothing
}catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
private void saveChanges(String selectedWH) {
try {
String file = prop.get(warehousesFile, selectedWH);
FileOutputStream fosTemp = new FileOutputStream(file+"temp");
ObjectOutputStream oosTemp = new ObjectOutputStream(fosTemp);
File oosOld = new File(file);
int compCount = mainPanel.getComponentCount();
for (int i = 0 ; i < compCount ; i++) {
Component comp = mainPanel.getComponent(i);
oosTemp.reset();
oosTemp.writeObject(comp);
}
oosTemp.close();
if (!oosOld.delete()) {
System.out.println("error while deleting");
}
new File(file+"temp").renameTo(oosOld);
repaint();
} catch (IOException iex) {
ex.printStackTrace();
}
}