从菜单中保存和打开对象

时间:2012-11-13 06:11:51

标签: java save

这是我的申请。我在保存对象并打开它们时遇到问题。当我尝试保存它时告诉我save.writeObject(firstName)中的变量无法解析为变量。

问题出在ActionPeformed块中:

 import java.awt.*;
 import java.awt.event.*;
 import java.io.File;
 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.io.Serializable;

 import javax.swing.*;
 import javax.swing.event.*;
 import javax.swing.border.*;
 import java.util.*;

  public class ClassRoomFrameTest 
{ 
public static void main(String[] args) 
{
    ClassRoomFrame frame = new ClassRoomFrame();
    frame.setSize(600,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
}
} 
class ClassRoomFrame extends JFrame implements ActionListener
{
private JPanel mainPanel = new JPanel();
private JPanel deptPanel = new JPanel(new GridLayout(2,3));
private JPanel studentPanel = new JPanel(new GridLayout(2,5));
private JPanel displayPanel = new JPanel(new BorderLayout());
    private JPanel buttonPanel  = new JPanel(new GridLayout(1,2));
    private JPanel menuBar = new JPanel();

private JTextArea  textArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane(textArea);

private JLabel classLocationLabel = new JLabel("Class Location");
private JLabel classRoomLabel     = new JLabel("Class Room Number");
private JLabel classCapacityLabel = new JLabel("Class Capacity");
private JTextField classLocationField = new JTextField();
private JTextField classRoomField     = new JTextField();
private JTextField classCapacityField = new JTextField();

private JLabel studentFNameLabel = new JLabel("First name");
private JLabel studentLNameLabel = new JLabel("Last name");
private JLabel studentIDLabel = new JLabel("ID Number");
private JLabel studentMajorLabel = new JLabel("Major");
private JLabel studentCreditsLabel = new JLabel("Credits");
private JTextField studentFNameField = new JTextField();
private JTextField studentLNameField = new JTextField();
private JTextField studentIDField = new JTextField();
private JTextField studentMajorField = new JTextField();
private JTextField studentCreditsField = new JTextField();

private JButton addButton     = new JButton("Add");
private JButton displayButton = new JButton("Display");

private JMenuBar menu = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem save = new JMenuItem("Open");
private JMenuItem open = new JMenuItem("Save");

private JFileChooser chooser = new JFileChooser();

Classroom room = null;


public ClassRoomFrame()
{
    deptPanel.setPreferredSize(new Dimension(600,50));
    deptPanel.setBorder(new EmptyBorder(new Insets(5,15,5,15)));

    deptPanel.add(classLocationLabel);
    deptPanel.add(classRoomLabel);
    deptPanel.add(classCapacityLabel);
    deptPanel.add(classLocationField);
    deptPanel.add(classRoomField);
    deptPanel.add(classCapacityField);

    fileMenu.add(fileMenu);
    fileMenu.add(open);
    fileMenu.add(save);

    menu.add(fileMenu);

    studentPanel.setBorder(new EmptyBorder(new Insets(5,15,5,15)));
    studentPanel.setPreferredSize(new Dimension(600,50));
    studentPanel.setBorder(new EmptyBorder(new Insets(5,15,5,15)));

    studentPanel.add(studentFNameLabel);
    studentPanel.add(studentLNameLabel);
    studentPanel.add(studentIDLabel);
    studentPanel.add(studentMajorLabel);
    studentPanel.add(studentCreditsLabel);
    studentPanel.add(studentFNameField);
    studentPanel.add(studentLNameField);
    studentPanel.add(studentIDField);
    studentPanel.add(studentMajorField);
    studentPanel.add(studentCreditsField);

    scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
    scrollPane.setPreferredSize(new Dimension(600,450));
    textArea.setBorder(new EmptyBorder(new Insets(5,15,5,15)));

    buttonPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
    buttonPanel.setPreferredSize(new Dimension(600, 50));
    buttonPanel.add(addButton);
    buttonPanel.add(displayButton);
    addButton.addActionListener(this);
    addButton.setActionCommand("Add");
    displayButton.addActionListener(this);
    displayButton.setActionCommand("Display");

    open.addActionListener(this);
    open.setActionCommand("Open");
    save.addActionListener(this);
    save.setActionCommand("Save");

    mainPanel.add(deptPanel);       
    mainPanel.add(studentPanel);        
    mainPanel.add(scrollPane);


    add(menu, BorderLayout.NORTH);
    add(mainPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);       
}

@SuppressWarnings("unused")
public void actionPerformed(ActionEvent e)
{
    /*---> HERE */if(e.getActionCommand().equals("Save"));
    {
        FileOutputStream saveFile = null;
        ObjectOutputStream save = null;

        try 
        {
            saveFile = new FileOutputStream("ObjectData.txt");
            save = new ObjectOutputStream(saveFile);

            /*--->Error*/ save.writeObject(index);

            save.writeObject(new Classroom());
        } catch (FileNotFoundException e1) 
        {

            e1.printStackTrace();
        } catch (IOException e1) 
        {

            e1.printStackTrace();
        }
        finally
        {
            try {saveFile.close();

            }catch(Exception exc){
                System.out.println(exc.getMessage());
            }
            }       

    }
    /*---> Here*/if (e.getActionCommand().equals("Open"))
    {
        ObjectInputStream in = null;
        int returnVal = chooser.showOpenDialog(ClassRoomFrame.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {
            File file = chooser.getSelectedFile();
            try {
        in = new ObjectInputStream(new FileInputStream(file));
            } catch (FileNotFoundException e1) 
            {

                e1.printStackTrace();
            } catch (IOException e1) 
            {

                e1.printStackTrace();
            }
        }
            try {
                Student st = (Student)in.readObject();
                Classroom cs = (Classroom)in.readObject();
                System.out.println(st);
                System.out.println(cs);

            } catch (IOException e1) {

                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {

                e1.printStackTrace();
            }
            try{in.close();}
            catch(Exception err){err.getMessage();}
    }

    if(e.getActionCommand().equals("Add"))
    {
        if(room == null)
        {
            room = new Classroom(classLocationField.getText(),
    Integer.parseInt(classRoomField.getText()),
           Integer.parseInt(classCapacityField.getText()));
            room.addStudent(
                   new Student(studentFNameField.getText(),      
                     studentLNameField.getText(),
                     studentIDField.getText(),
                     studentMajorField.getText(),
               Integer.parseInt(studentCreditsField.getText())));

            classLocationField.setEditable(false);
            classRoomField.setEditable(false);
            classCapacityField.setEditable(false);
            studentFNameField.setText("");       
            studentLNameField.setText("");
            studentIDField.setText("");
            studentMajorField.setText("");
                            studentCreditsField.setText("");
            textArea.setText("Class and first student added.");
        }
        else
        {
            room.addStudent(
                   new Student(studentFNameField.getText(),      
                     studentLNameField.getText(),
                     studentIDField.getText(),
                     studentMajorField.getText(),
               Integer.parseInt(studentCreditsField.getText())));
            textArea.setText("Next student added.");

            studentFNameField.setText("");       
            studentLNameField.setText("");
            studentIDField.setText("");
            studentMajorField.setText("");
        studentCreditsField.setText("");
        }           
    }
    else if(e.getActionCommand().equals("Display"))
    {
        if (room != null)
        {
            textArea.setText(room.toString());
        }
        else
        {
            textArea.setText("Nothing to display");
        }   
    }
}
}

class Student implements Serializable
{
public String firstName, lastName, studentIdNumber, studentMajor;
public int totalCourseCredits;

//-----------------------------------------------------------------
// Create an empty studentusing a default constructor.
//-----------------------------------------------------------------
public Student ()
{
}
//-----------------------------------------------------------------
// Creates a Student with the specified information.
//-----------------------------------------------------------------
public Student (String name1, String name2, String identification, 
                String myMajor, int myTotalCredits)
{
    firstName = name1;
    lastName = name2;
    studentIdNumber = identification;
    studentMajor = myMajor;
    totalCourseCredits = myTotalCredits;
}

//-----------------------------------------------------------------
// Gets and sets first name.
//-----------------------------------------------------------------
public void setFirstName()
{
    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter your First Name: ");
    firstName = scan.nextLine();
}

//-----------------------------------------------------------------
// Gets and sets last name.
//-----------------------------------------------------------------
public void setLastName()
{
    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter your Last Name: ");
    lastName = scan.nextLine();
}

//-----------------------------------------------------------------
// Gets and sets Total Course Credits.
//-----------------------------------------------------------------
public void setTotalCredits()
{
    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter your Total Credits: ");
    totalCourseCredits = scan.nextInt();
}

//-----------------------------------------------------------------
// Gets and sets Student ID Number.
//-----------------------------------------------------------------
public void setIdNumber()
{
    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter your ID Number: ");
    studentIdNumber = scan.nextLine();
}


//-----------------------------------------------------------------
// Gets and sets Student Major.
//-----------------------------------------------------------------
public void setMajor()
{
    Scanner scan = new Scanner (System.in);

    System.out.println ("Enter your Major: ");
    studentMajor = scan.nextLine();
}

 public String toString()
 {
    String s = "First name:     " + firstName + "\n" + 
               "Last name:      " + lastName + "\n" + 
               "StudentID:      " + studentIdNumber + "\n" + 
               "Student Major:  " + studentMajor + "\n" + 
                "Course Creidts: " + totalCourseCredits + "\n"; 
    return s;
}
}

   class Classroom implements Serializable
  {

    private Student[] classRoster;
private int index = 0;
    private int capacityStudents, roomNumber;
    private String buildingLocation;

  //-----------------------------------------------------------------
  // Creates an empty Classroom.
  //-----------------------------------------------------------------
public Classroom()
{
    capacityStudents = 0;
  roomNumber = 0;
  buildingLocation = "";
}

  //-----------------------------------------------------------------
  // Creates a Classroom with the specified information.
  //-----------------------------------------------------------------
public Classroom(String location, int room, int cap)
{
    capacityStudents = cap;
  roomNumber = room;
  buildingLocation = location;
  classRoster = new Student[capacityStudents];
}


  //-----------------------------------------------------------------
  // Gets and sets Building Location.
 //-----------------------------------------------------------------
  public void setBuilding()
 {
  Scanner scan = new Scanner (System.in);

  System.out.println ("Enter the Building Location: ");
  buildingLocation = scan.next();
 }

  //-----------------------------------------------------------------
  // Gets and sets Room Number.
  //-----------------------------------------------------------------
public void setRoomNumber()
{
  Scanner scan = new Scanner (System.in);

  System.out.println ("Enter the Room Number: ");
  roomNumber = scan.nextInt();
}

  //-----------------------------------------------------------------
  // Sets Capacity of Students.
  //-----------------------------------------------------------------
public void setCapacityStudents()
{
  Scanner scan = new Scanner (System.in);

  System.out.println ("Enter The Capacity of the Classroom: ");
  capacityStudents = scan.nextInt();
  classRoster = new Student[capacityStudents];
}

  //-----------------------------------------------------------------
  // Gets Capacity of Students.
 //-----------------------------------------------------------------
public int getCapacityStudents()
{
  return capacityStudents;
}

  //-----------------------------------------------------------------
 // Adds an Individual Student to the Classroom, checking if the 
 // capacity of the clasroom is full.
 //-----------------------------------------------------------------
public void addStudent (Student student)
{
    if(index < capacityStudents)
    {
      classRoster[index] = student;
      index++;
    }
    else
    {
      System.out.println("Capacity exceeded! - Student cannot be added.");
    }   
}

 //-----------------------------------------------------------------
 // Adds an Individual Student to the Classroom, checking if the 
 // capacity of the clasroom is full.
 //-----------------------------------------------------------------
  public String toString()
{
    StringBuffer sb = new StringBuffer
                          ("Building:     " + buildingLocation +
                           "\nClass room: " + roomNumber +
                "\nCapacity:   " + capacityStudents + "\n\n"
                                 ); 
    for(int i = 0; i < classRoster.length; i++)
    {
        if(classRoster[i] != null)
          sb.append(classRoster[i].toString() + "\n");
    }
    return sb.toString();
   }
  }

2 个答案:

答案 0 :(得分:1)

错误告诉您索引在save方法的范围内不可用。如果要保存索引,则需要在save方法中执行save.writeObject( classroom.getIndex())之类的操作。

答案 1 :(得分:1)

您已在index课程中声明ClassRoom变量。但是您尝试在actionPerformed类的方法ClassRoomFrame中使用它。

由于index的范围仅限于它的封闭类,因此如果不在ClassRoom类上使用实例,它将无法在其他类中显示。这就是编译器错误所说的:cannot find symbol

相关问题