如何在java

时间:2016-12-04 07:50:24

标签: java user-interface jpanel clickable

我有可点击的JPanel网格,可以更改每个面板的颜色。我有两个按钮存储和恢复以保存和检索对象。在附带的屏幕截图中,我选择了第一行,第二列并将其存储到文件中。我试图选择第一行,第三列并单击还原按钮以还原以前保存的数据 - 这应该是第一行,第二列只选择。但它似乎没有恢复。

以下是代码:

private class StoreRestoreButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == buttonStore) {
            if (textFileName.getText().equals("")) {
                labelCurrentStatus.setText("Please input the filename to store");
            } else {
                // Write display list array list to an object output stream.
                  try {
                    FileOutputStream fos = new FileOutputStream(textFileName.getText());
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    //displayList is an ArrayList of objects
                    oos.writeObject(displayList);
                    oos.flush();
                    oos.close();
                    fos.close();
                  } catch (IOException ex) {
                     labelCurrentStatus.setText("Error saving file");
                    System.err.println("Error saving file");
                  }
            }

        } else if (e.getSource() == buttonRestore) {
             if (textFileName.getText().equals("")) {
                 labelCurrentStatus.setText("Please input the filename to restore");
             } else {
                 // Read a new display list array list from an object input stream.
                  try {
                    FileInputStream fis = new FileInputStream(textFileName.getText());
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    //displayList is an ArrayList of Room objects
                    displayList = (ArrayList<Room>) ois.readObject();
                    rebuildGridPanel(displayList);
                    ois.close();
                    fis.close();
                    repaint();
                  } catch (IOException ex) {
                    System.err.println("Trouble reading file");
                    labelCurrentStatus.setText("Trouble reading file");
                  } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
             }

        }

    }
}

}

enter image description here

0 个答案:

没有答案