这个程序编译通过了,运行出来结果了但是添加数据不行
  以前那个只要语文、数学、英语,能通过 我加了两个 物理和化学  请问程序哪里错了
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; public class StudentInformation implements ActionListener { 
Frame mainFrame; 
Frame inputFrame; 
Frame searchFrame; 
TextArea stuInfo; 
Label lb[] = new Label[9]; 
Label lb2[] = new Label[2]; 
TextField tf[] = new TextField[11]; 
TextField tf2[] = new TextField[2]; 
Button btn[] = new Button[3]; 
Button btn2[] = new Button[2]; 
Panel p1, p2, p3, p4; 
IOOperation ioo; 
Student stu; 
Student student[] = new Student[100]; public StudentInformation() { 
/** 
* 设置主菜单 
*/ 
mainFrame = new Frame("学生信息管理"); 
mainFrame.addWindowListener(new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
System.exit(0); 

}); MenuItem item1 = new MenuItem("记录"); 
MenuItem item2 = new MenuItem("查找"); 
MenuItem item3 = new MenuItem("修改"); 
MenuItem item4 = new MenuItem("退出"); 
MenuItem item5 = new MenuItem("关于"); 
item1.addActionListener(this); 
item2.addActionListener(this); 
item3.addActionListener(this); 
item4.addActionListener(this); 
item5.addActionListener(this); Menu menu1 = new Menu("文件"); 
menu1.add(item1); 
menu1.add(item2); 
menu1.add(item3); 
menu1.addSeparator(); 
menu1.add(item4); Menu menu2 = new Menu("帮助"); 
menu2.add(item5); MenuBar mb = new MenuBar(); 
mb.add(menu1); 
mb.add(menu2); 
mainFrame.setMenuBar(mb); stuInfo = new TextArea(); 
stuInfo.setFont(new Font("serif", Font.PLAIN, 18)); 
mainFrame.add(stuInfo); mainFrame.setSize(400, 250); 
mainFrame.setLocation(200, 100); 
mainFrame.setVisible(true); /** 
* 设置输入框架用来存入学生信息
*/ 
inputFrame = new Frame(); 
inputFrame.addWindowListener(new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
inputFrame.setVisible(false); 

}); p1 = new Panel(new GridLayout(9, 2)); 
p2 = new Panel(); 
String lbname[] = {"学号:", "姓名:", "性别:", "出生年月:", 
"班级:", "语文:", "数学:", "英语:","物理:","化学:", "总分:"}; 
String btnname[] = {"保存", "删除", " 退出 "}; 
for(int i=0; i<11; i++) { 
lb[i] = new Label(lbname[i]); 
tf[i] = new TextField(15); 
p1.add(lb[i]); 
p1.add(tf[i]); 

for(int i=0; i<3; i++) { 
btn[i] = new Button(btnname[i]); 
btn[i].addActionListener(this); 
p2.add(btn[i]); 

btn[2].setActionCommand("输入"); 
inputFrame.add(p1, BorderLayout.CENTER); 
inputFrame.add(p2, BorderLayout.SOUTH); inputFrame.pack(); 
inputFrame.setLocationRelativeTo(mainFrame); /** 
* 设置查找框架用来查找学生信息
*/ 
searchFrame = new Frame("查找学生"); 
searchFrame.addWindowListener(new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
searchFrame.setVisible(false); 

}); p3 = new Panel(new GridLayout(2, 2)); 
p4 = new Panel(); 
String lbname2[] = {"学号:", "姓名:"}; 
String btnname2[] = {"查找", " 退出 "}; 
for(int i=0; i<2; i++) { 
lb2[i] = new Label(lbname2[i]); 
tf2[i] = new TextField(15); 
p3.add(lb2[i]); 
p3.add(tf2[i]); 

for(int i=0; i<2; i++) { 
btn2[i] = new Button(btnname2[i]); 
btn2[i].addActionListener(this); 
p4.add(btn2[i]); 

btn2[1].setActionCommand("查找"); 
searchFrame.add(p3, BorderLayout.CENTER); 
searchFrame.add(p4, BorderLayout.SOUTH); searchFrame.pack(); 
searchFrame.setLocationRelativeTo(mainFrame); /** 
* 输入输出操作对象 
*/ 
ioo = new IOOperation(); 
student = ioo.getAllStudent(); } 

解决方案 »

  1.   

    public void actionPerformed(ActionEvent e) { 
    /** 
    * 菜单栏目功能
    */ 
    if (e.getSource() instanceof MenuItem) { 
    MenuItem mi = (MenuItem) e.getSource(); 
    if (mi.getLabel().equals("记录")) { 
    inputFrame.setTitle("记录"); 
    for(int i=0; i<9; i++) 
    tf[i].setText(""); 
    p2.remove(btn[1]); 
    btn[0].setActionCommand("输入"); 
    inputFrame.setVisible(true); 

    else if (mi.getLabel().equals("查找")) { 
    searchFrame.setVisible(true); 

    else if (mi.getLabel().equals("修改")) { 
    inputFrame.setTitle("修改"); 
    if(stu != null) { 
    tf[0].setText(stu.getCode()); 
    tf[1].setText(stu.getName()); 
    tf[2].setText(stu.getSex()); 
    tf[3].setText(stu.getBirthPlace()); 
    tf[4].setText(stu.getStuClass()); 
    tf[5].setText(stu.getChinese() + ""); 
    tf[6].setText(stu.getMath() + ""); 
    tf[7].setText(stu.getEnglish() + ""); 
    tf[8].setText(stu.getPhysics() + "");
    tf[9].setText(stu.getChemistry() + "");
    tf[10].setText(stu.getTotalScore() + ""); 

    p2.remove(btn[2]); 
    p2.add(btn[1]); 
    p2.add(btn[2]); 
    btn[0].setActionCommand("修改"); 
    inputFrame.setVisible(true); 

    else if (mi.getLabel().equals("退出")) 
    System.exit(0); 
    else if (mi.getLabel().equals("关于")) { 
    final Dialog progInfo = new Dialog(mainFrame, "ProgInfo", true); 
    progInfo.addWindowListener(new WindowAdapter() { 
    public void windowClosing(WindowEvent e) { 
    progInfo.dispose(); 

    }); 
    progInfo.setLayout(new FlowLayout()); 
    Label l = new Label("学生信息系统"); 
    progInfo.add(l); 
    progInfo.setSize(200, 80); 
    progInfo.setLocationRelativeTo(mainFrame); 
    progInfo.setVisible(true); 


    /** 
    * 按钮功能
    */ 
    else { 
    Button btn = (Button)e.getSource(); 
    if(btn.getLabel().equals("保存")) { 
    if(!tf[0].getText().equals("") && 
    !tf[1].getText().equals("") && 
    !tf[2].getText().equals("") && 
    !tf[3].getText().equals("") && 
    !tf[4].getText().equals("") && 
    !tf[5].getText().equals("") && 
    !tf[6].getText().equals("") && 
    !tf[7].getText().equals("") && 
    !tf[8].getText().equals("") &&
    !tf[9].getText().equals("") &&
    !tf[10].getText().equals("")) { 
    Student s = new Student(tf[0].getText(), tf[1].getText(), 
    tf[2].getText(), tf[3].getText(), tf[4].getText(), 
    Integer.parseInt(tf[5].getText()), 
    Integer.parseInt(tf[6].getText()), 
    Integer.parseInt(tf[7].getText()),
    Integer.parseInt(tf[8].getText()),
    Integer.parseInt(tf[9].getText()), 
    Integer.parseInt(tf[10].getText())); 
    if(btn.getActionCommand().equals("输入")) { 
    for(int i=0; i<student.length; i++) { 
    if(student[i] == null) { 
    student[i] = s; 
    break; 


    ioo.write(student); 

    else { 
    for(int i=0; i<student.length; i++) { 
    if(student[i].equals(stu)) { 
    student[i] = s; 
    break; 


    ioo.write(student); 


    inputFrame.setVisible(false); 
    stuInfo.setText(""); 

    else if(btn.getLabel().equals("删除")) { 
    int index = 200; 
    if(stu != null) { 
    for(int i=0; i<student.length; i++) { 
    if(student[i]!=null && student[i].equals(stu)) { 
    index = i; 
    if(i != student.length-1) 
    student[i] = student[i+1]; 
    else 
    student[i] = null; 

    if(i==index && student[i+1]==null) 
    break; 
    else if(i>index && i<student.length-1) { 
    student[i] = student[i+1]; 
    if(i == student.length - 1) 
    student[i] = null; 


    ioo.write(student); 

    stu = null; 
    inputFrame.setVisible(false); 
    stuInfo.setText(""); 
    /*for(int i=0; i<student.length; i++) { 
    if(student[i]!=null) 
    System.out.println(i + " " + student[i].getCode()); 
    }*/ 

    else if(btn.getLabel().equals("查找")) { 
    stu = null; 
    if(!tf2[0].getText().equals("") || 
    !tf2[1].getText().equals("")) { 
    String condition = ""; 
    if(!tf2[0].getText().equals("")) { 
    condition = tf2[0].getText(); 

    else 
    condition = tf2[1].getText(); 
    for(int i=0; i<student.length; i++) { 
    if (student[i] != null) { 
    if (student[i].getCode().equals(condition) || 
    student[i].getName().equals(condition)) { 
    stu = student[i]; 
    break; 




    if(stu != null) { 
    stuInfo.setText("学号: " + stu.getCode() + "\n" + 
    "姓名: " + stu.getName() + "\n" + 
    "性别: " + stu.getSex() + "\n" + 
    "出生年月: " + stu.getBirthPlace() + "\n" + 
    "语文: " + stu.getChinese() + "\n" +
    "数学: " + stu.getMath() + "\n" + 
    "英语: " + stu.getEnglish() + "\n" + 
    "物理: " + stu.getPhysics() + "\n" +
    "化学: " + stu.getChemistry() + "\n" +
    "总分: " + stu.getTotalScore()); 

    searchFrame.setVisible(false); 

    else if(btn.getLabel().equals(" 退出 ")) 
    if(btn.getActionCommand().equals("输入")) 
    inputFrame.setVisible(false); 
    else 
    searchFrame.setVisible(false); 

    } public static void main(String[] args) { 
    new StudentInformation(); 

    } /** 
    * 用来存放学生信息的类
    */ 
    class Student implements Serializable { 
    private String code; 
    private String name; 
    private String sex; 
    private String birthPlace; 
    private String stuClass; 
    private int Chinese; 
    private int Math; 
    private int English; 
    private int Physics;
    private int Chemistry;
    private int totalScore; 
      

  2.   

    public Student(String code, String name, String sex, String birthPlace, 
    String stuClass, int chinese, int math, int english,int physics,int chemistry, int totalScore) { 
    super(); 
    this.code = code; 
    this.name = name; 
    this.sex = sex; 
    this.birthPlace = birthPlace; 
    this.stuClass = stuClass; 
    Chinese = chinese; 
    Math = math; 
    English = english; 
    Physics = physics;
    Chemistry = chemistry;
    this.totalScore = totalScore; 
    } public String getBirthPlace() { 
    return birthPlace; 
    } public int getChinese() { 
    return Chinese; 
    } public String getCode() { 
    return code; 
    } public int getEnglish() { 
    return English; 
    } public int getMath() { 
    return Math; 
    } public int getPhysics() {
    return Physics;
    }public int getChemistry() {
    return Chemistry;
    }

    public String getName() { 
    return name; 
    } public String getSex() { 
    return sex; 
    } public String getStuClass() { 
    return stuClass; 
    } public int getTotalScore() { 
    return totalScore; 
    } public boolean equals(Object obj) { 
    if(obj != null && (obj instanceof Student)) 
    if(this.getCode().equals(((Student)obj).getCode()) && 
    this.getName().equals(((Student)obj).getName()) && 
    this.getSex().equals(((Student)obj).getSex()) && 
    this.getBirthPlace().equals(((Student)obj).getBirthPlace()) && 
    this.getStuClass().equals(((Student)obj).getStuClass()) && 
    this.getChinese() == ((Student)obj).getChinese() && 
    this.getMath() == ((Student)obj).getMath() && 
    this.getEnglish() == ((Student)obj).getEnglish() && 
    this.getPhysics() == ((Student)obj).getPhysics() && 
    this.getChemistry() == ((Student)obj).getChemistry() && 
    this.getTotalScore() == ((Student)obj).getTotalScore()) 
    return true; 
    return false; 

    } /** 
    * 用来进行输入输出操作的类
    */ 
    class IOOperation { 
    private File file = new File("C:\\stuinfo.txt"); public IOOperation() { 
    try { 
    if(!file.exists()) 
    file.createNewFile(); 
    } catch (IOException e) { 
    e.printStackTrace(); 

    } /** 
    * 写入文件 
    */ 
    public void write(Student[] s) { 
    try { 
    FileOutputStream fos = new FileOutputStream(file); 
    ObjectOutputStream objOut = new ObjectOutputStream(fos); 
    objOut.writeObject(s); 
    objOut.close(); 
    fos.close(); 
    } catch(Exception e) { 
    e.printStackTrace(); 

    } /** 
    * 从文件中读取学生信息
    */ 
    public Student[] getAllStudent() { 
    Student ss[] = new Student[100]; 
    try { 
    if (file.length() > 0) { 
    FileInputStream fis = new FileInputStream(file); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    ss = (Student[]) ois.readObject(); 
    ois.close(); 
    fis.close(); 

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

    return ss; 

    }