http://www.cnblogs.com/Min21/p/7064093.html

  1. 负责person类的编写,建立person对象,
  2. 完成Menu中增删改查的功能。

  1. 1、建立对象建立可序列化对象person,并构建函数。
  2. 主要代码:
  3. import java.io.Serializable;
  4. public class Person implements Serializable{
  5. private String num;
  6. private String name;
  7. private String dor;
  8. private String address;
  9. private String sex;
  10. private String date;
  11. private String pol;
  12. private String phone;
  13. public Person(){}
  14. public Person(String num,String name,String dor,String address,String sex,String date,String pol,String phone ){
  15. this.num=num;
  16. this.name=name;
  17. this.dor=dor;
  18. this.address=address;
  19. this.sex=sex;
  20. this.date=date;
  21. this.pol=pol;
  22. this.phone=phone;
  23. }
  24. public void setNum(String num){
  25. this.num=num;
  26. }
  27. public String getNum(){
  28. return num;
  29. }
  30. public void setName(String name){
  31. this.name=name;
  32. }
  33. public String getName(){
  34. return name;
  35. }
  36. public void setDor(String dor){
  37. this.dor=dor;
  38. }
  39. public String getDor(){
  40. return dor;
  41. }
  42. public void setAddress(String address){
  43. this.address=address;
  44. }
  45. public String getAddress(){
  46. return address;
  47. }
  48. public void setSex(String sex){
  49. this.sex=sex;
  50. }
  51. public String getSex(){
  52. return sex;
  53. }
  54. public void setDate(String date){
  55. this.date=date;
  56. }
  57. public String getDate(){
  58. return date;
  59. }
  60. public void setPol(String pol){
  61. this.pol=pol;
  62. }
  63. public String getPol(){
  64. return pol;
  65. }
  66. public void setPhone(String phone){
  67. this.phone=phone;
  68. }
  69. public String getPhone(){
  70. return phone;
  71. }
  72. }
  73. 2.增加学生信息功能
  74. but1.addActionListener(new ActionListener() { // 增加,内部类//进行某项操作时触发功能
  75. public void actionPerformed(ActionEvent e) {//用于接收操作事件的侦听器接口
  76. if (e.getSource() == but1) {
  77. but3.setEnabled(false);//使but3这个按钮变灰不可点击了
  78. String number1 = number.getText();
  79. if (number1.length() == 12) {
  80. try {
  81. ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));//读回对象
  82. has = (Hashtable) in.readObject(); in.close();
  83. } catch (Exception e1) {
  84. }
  85. } else {
  86. JOptionPane.showMessageDialog(null, "请输入12位数字的学号");//提示框显示
  87. }
  88. //
  89. if (number1.length() == 12) {
  90. if (has.containsKey(number1)) { JOptionPane.showMessageDialog(null, "该生信息已存在,请到修改页面修改!");
  91. } else {
  92. String name1 = name.getText();
  93. String dor1 = dor.getText();
  94. String address1 = address.getText();
  95. String sex1 = sex.getText();
  96. String date1 = date.getText();
  97. String pol1 = pol.getText();
  98. String phone1 = phone.getText();
  99. Person per = null;
  100. per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
  101. has.put(number1, per);// ???
  102. try {
  103. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
  104. out.writeObject(has);
  105. out.close();
  106. JOptionPane.showMessageDialog(null, "添加成功!");
  107. } catch (Exception e1) {}
  108. }
  109. }
  110. }
  111. }
  112. 3.)修改功能:为了防止误修改,首先要查看,才能修改,查看后直接改输入栏中的数据,点击修改,既修改成功。
  113. but3.addActionListener(new ActionListener() { // 修改
  114. public void actionPerformed(ActionEvent e) {
  115. if (e.getSource() == but3) {
  116. but3.setEnabled(false);
  117. String number1 = number.getText();
  118. String name1 = name.getText();
  119. String dor1 = dor.getText();
  120. String address1 = address.getText();
  121. String sex1 = sex.getText();
  122. String date1 = date.getText();
  123. String pol1 = pol.getText();
  124. String phone1 = phone.getText();
  125. Person per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
  126. has.put(number1, per);
  127. JOptionPane.showMessageDialog(null, "修改成功");
  128. try {
  129. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
  130. out.writeObject(has);
  131. out.close();
  132. } catch (Exception e1) {
  133. }
  134. }
  135. }
  136. });
  137. 4.查看功能:首先输入要查看的学号,点查看,信息将会显示在输入栏中。如果学号不存在,下面会有提示。
  138. but4.addActionListener(new ActionListener() { // 查看
  139. public void actionPerformed(ActionEvent e) {
  140. if (e.getSource() == but4) {
  141. but3.setEnabled(false);
  142. String number1 = number.getText();
  143. if (number1.length() == 12) {
  144. if (has.containsKey(number1)) {
  145. try {
  146. ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
  147. has = (Hashtable) in.readObject();
  148. in.close();
  149. } catch (Exception e1) {
  150. }
  151. Person per = (Person) has.get(number1);
  152. name.setText(per.getName());
  153. dor.setText(per.getDor());
  154. address.setText(per.getAddress());
  155. sex.setText(per.getSex());
  156. date.setText(per.getDate());
  157. pol.setText(per.getPol());
  158. phone.setText(per.getPhone());
  159. but3.setEnabled(true);
  160. } else {
  161. JOptionPane.showMessageDialog(null, "学号不存在");
  162. }
  163. } else {
  164. JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
  165. }
  166. }
  167. }
  168. });
  169. 5.5)删除功能:先输入要删除的学号,点删除,该学生的信息将被移除,在查看该学号,将不存在。
  170. but2.addActionListener(new ActionListener() { // 删除
  171. public void actionPerformed(ActionEvent e) {
  172. if (e.getSource() == but2) {
  173. but3.setEnabled(false);
  174. String number1 = number.getText();
  175. if (number1.length() == 12) {
  176. try {
  177. ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
  178. has = (Hashtable) in.readObject();
  179. in.close();
  180. } catch (Exception e1) {
  181. }
  182. } else {
  183. JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
  184. }
  185. if (has.containsKey(number1)) {
  186. has.remove(number1);
  187. ObjectOutputStream out = null;
  188. JOptionPane.showMessageDialog(null, "删除成功");
  189. try {
  190. out = new ObjectOutputStream(new FileOutputStream(file));
  191. //out.writeObject(has);
  192. out.close();
  193. } catch (IOException ex) {
  194. Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);//记录当前类可能发生的异常
  195. }
  196. } else {
  197. JOptionPane.showMessageDialog(null, "学号不存在");
  198. }
  199. }
  200. }
  201. });
  1. 我们打算做文本io时想实现内存与文本的交互一直无法成功,后来百度查了一下是需要将数据可序列化才能行,而且还要用ObjectOutputStreamObjectInputStream来传递数据,而它们对象序列话是在它们中实现的,这些我们都没学过。还好书上有,网上也有它们的解析,于是我们就一点一点的尝试,最终实现内存与文本的交互并把数据存到文本里。

版权声明:本文为lamfinethinkyou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lamfinethinkyou/p/7063919.html