回复 11楼 llooppzhang
现在把两个模块联系起来 我试着增加成员函数 提供修改成员变量的方式 达到list具有相同的内容 没用 好像运行机制不一样
import java.awt.BorderLayout; import java.awt.Button; import java.awt.CardLayout; import java.awt.Frame; import java.awt.Label; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import import import import import import import import java.util.Iterator; import java.util.LinkedList; import javax.swing.Box; public class main { public static void main(String args[]) { new yxf(); } } class student implements Serializable { student(String id, String name, double mark) { m_id = new String(id); m_name = new String(name); m_mark = mark; } String show_id (){ return m_id;} String show_name() {return m_name;} double show_mark() { return m_mark;} private String m_id = null;//学生的学号 private String m_name = null;//学生的姓名 private double m_mark = 0;//学生的成绩 } class yxf extends Frame implements ActionListener { add panel_add = null; show panel_show = null; Delete panel_delete = null; MenuBar menubar = null; Menu menu = null; MenuItem menu_add = null; MenuItem menu_show = null; MenuItem menu_del = null; CardLayout card = null; yxf() { super("学生管理系统"); card = new CardLayout(); setLayout(card); panel_add = new add(); add("添加",panel_add); panel_show = new show(); add("显示", panel_show); panel_delete = new Delete(); add("删除", panel_delete); menu = new Menu("功能"); menu_add = new MenuItem("添加"); menu_add.addActionListener(this); menu_show = new MenuItem("显示"); menu_show.addActionListener(this); menu_del = new MenuItem("删除"); menu_del.addActionListener(this); menu.add(menu_add); menu.add(menu_show); menu.add(menu_del); menubar = new MenuBar(); menubar.add(menu); setMenuBar(menubar); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(100, 100, 280, 200); setVisible(true); validate(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == menu_add) { card.show(this, "添加"); setSize(280, 200); } else if (e.getSource() == menu_show) { card.show(this, "显示"); setSize(280, 300); } else if (e.getSource() == menu_del) { card.show(this, "删除"); setSize(280, 200); } } } //定义添加信息的类 class add extends Panel implements ActionListener { LinkedList<student> list = null; Button ack = null;//确认按钮 Button cancel = null;//取消按钮 TextField tf_id; TextField tf_name; TextField tf_mark; file_do fd = new file_do(); add() { Box box1 = Box.createVerticalBox(); Box box2 = Box.createVerticalBox(); box1.add(new Label("学号:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("姓名:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("成绩:")); box1.add(Box.createVerticalStrut(4)); cancel = new Button("重置"); cancel.addActionListener(this); box1.add(cancel); tf_id = new TextField(10); tf_name = new TextField(10); tf_mark = new TextField(10); box2.add(tf_id); box2.add(Box.createVerticalStrut(4)); box2.add(tf_name); box2.add(Box.createVerticalStrut(4)); box2.add(tf_mark); box2.add(Box.createVerticalStrut(4)); ack = new Button("确定"); ack.addActionListener(this); box2.add(ack); add(box1); add(box2); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == cancel) { tf_id.setText(null); tf_name.setText(null); tf_mark.setText(null); } else if (e.getSource() == ack) { if (null == list) { list = new LinkedList<student>(); } if (0 != tf_id.getText().toString().trim().length() && 0 != tf_name.getText().toString().trim().length() && 0 != tf_mark.getText().toString().trim().length()) { list_add(); list = fd.write_file(list); tf_id.setText(null); tf_name.setText(null); tf_mark.setText(null); } } } //向链表中添加数据 void list_add() { String str_id = new String(tf_id.getText()); String str_name = new String(tf_name.getText()); double dou = 0; try { dou = Double.parseDouble(tf_mark.getText()); list.add(new student(str_id, str_name, dou)); } catch (NumberFormatException e) { System.out.println("错误:"+e.getMessage()); } } } //定义显示信息的类 class show extends Panel implements ActionListener { LinkedList<student> list = new LinkedList<student>(); TextArea ta_show = null; Button fl = null; file_do fd = new file_do(); show() { fl = new Button("刷新"); fl.addActionListener(this); ta_show = new TextArea(10, 30); add(ta_show, BorderLayout.CENTER); add(fl, BorderLayout.SOUTH); show_list(); setVisible(true); validate(); } public void show_list() { if (null == list) { ta_show.append("没有数据\n"); return; } try { Iterator<student> iter = list.iterator(); ta_show.setText(""); while (iter.hasNext()) { student temp = (student)iter.next(); ta_show.append("学号:"+temp.show_id()+" 姓名:"+temp.show_name()+" 成绩:"+temp.show_mark()+"\n"); } } catch (NullPointerException e) { System.out.println("Iterator " +e.getMessage()); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == fl) { list = fd.read_file(list); show_list(); } } } class Delete extends Panel implements ActionListener { LinkedList<student> list = null; Button ack = null;//确认按钮 Button cancel = null;//取消按钮 TextField tf_id; TextField tf_name; TextField tf_mark; file_do fd = new file_do(); Delete() { Box box1 = Box.createVerticalBox(); Box box2 = Box.createVerticalBox(); box1.add(new Label("学号:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("姓名:")); box1.add(Box.createVerticalStrut(4)); box1.add(new Label("成绩:")); box1.add(Box.createVerticalStrut(4)); cancel = new Button("取消"); cancel.addActionListener(this); box1.add(cancel); tf_id = new TextField(10); tf_name = new TextField(10); tf_mark = new TextField(10); box2.add(tf_id); box2.add(Box.createVerticalStrut(4)); box2.add(tf_name); box2.add(Box.createVerticalStrut(4)); box2.add(tf_mark); box2.add(Box.createVerticalStrut(4)); ack = new Button("删除"); ack.addActionListener(this); box2.add(ack); add(box1); add(box2); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == cancel) { tf_id.setText(null); tf_name.setText(null); tf_mark.setText(null); } else if (e.getSource() == ack) { if (null == list) { list = new LinkedList<student>(); } if (0 != tf_id.getText().toString().trim().length() && 0 != tf_name.getText().toString().trim().length() && 0 != tf_mark.getText().toString().trim().length()) { list = fd.read_file(list); delete_list(); list = fd.write_file(list); tf_id.setText(null); tf_name.setText(null); tf_mark.setText(null); } } } void delete_list() { String str_id = new String(tf_id.getText()); String str_name = new String(tf_name.getText()); double dou = 0; try { dou = Double.parseDouble(tf_mark.getText()); student stu = new student(str_id, str_name, dou); for (int i=0; i<list.size(); ++i) { if (((student)list.get(i)).equals(stu)) { list.remove(i); --i; } } } catch (NumberFormatException e) { System.out.println("错误:"+e.getMessage()); } } } class file_do { LinkedList<student> write_file(LinkedList<student> list) { try { FileOutputStream out = new FileOutputStream("yxf.txt"); ObjectOutputStream object_out = new ObjectOutputStream(out); Iterator<student> iter = list.iterator(); while(iter.hasNext()) { student stu = (student)iter.next(); object_out.writeObject(stu); } out.close(); object_out.close(); } catch (IOException e) { e.printStackTrace(); } return list; } LinkedList<student> read_file(LinkedList<student> list) { list.clear(); try { FileInputStream in = new FileInputStream("yxf.txt"); ObjectInputStream object_in = new ObjectInputStream(in); try { while (true) { student stu = (student)object_in.readObject(); list.add(stu); } } catch(EOFException e) { System.out.println("文件已读完"); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { in.close(); object_in.close(); } } catch (IOException e) { System.out.println("文件操作:"+e.getMessage()); } return list; } }