[Java] 学生信息 →→→→→进入此内容的聊天室

来自 , 2019-09-28, 写在 Java, 查看 155 次.
URL http://www.code666.cn/view/36d75342
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4.  
  5. public class StudentInformation implements ActionListener {
  6. Frame mainFrame;
  7. Frame inputFrame;
  8. Frame searchFrame;
  9. TextArea stuInfo;
  10. Label lb[] = new Label[9];
  11. Label lb2[] = new Label[2];
  12. TextField tf[] = new TextField[9];
  13. TextField tf2[] = new TextField[2];
  14. Button btn[] = new Button[3];
  15. Button btn2[] = new Button[2];
  16. Panel p1, p2, p3, p4;
  17. IOOperation ioo;
  18. Student stu;
  19. Student student[] = new Student[100];
  20.  
  21. public StudentInformation() {
  22. /**
  23. * set mainframe
  24. */
  25. mainFrame = new Frame("Student Information");
  26. mainFrame.addWindowListener(new WindowAdapter() {
  27. public void windowClosing(WindowEvent e) {
  28. System.exit(0);
  29. }
  30. });
  31.  
  32. MenuItem item1 = new MenuItem("Record");
  33. MenuItem item2 = new MenuItem("Search");
  34. MenuItem item3 = new MenuItem("Modify");
  35. MenuItem item4 = new MenuItem("Exit");
  36. MenuItem item5 = new MenuItem("About");
  37. item1.addActionListener(this);
  38. item2.addActionListener(this);
  39. item3.addActionListener(this);
  40. item4.addActionListener(this);
  41. item5.addActionListener(this);
  42.  
  43. Menu menu1 = new Menu("File");
  44. menu1.add(item1);
  45. menu1.add(item2);
  46. menu1.add(item3);
  47. menu1.addSeparator();
  48. menu1.add(item4);
  49.  
  50. Menu menu2 = new Menu("Help");
  51. menu2.add(item5);
  52.  
  53. MenuBar mb = new MenuBar();
  54. mb.add(menu1);
  55. mb.add(menu2);
  56. mainFrame.setMenuBar(mb);
  57.  
  58. stuInfo = new TextArea();
  59. stuInfo.setFont(new Font("serif", Font.PLAIN, 18));
  60. mainFrame.add(stuInfo);
  61.  
  62. mainFrame.setSize(400, 250);
  63. mainFrame.setLocation(200, 100);
  64. mainFrame.setVisible(true);
  65.  
  66. /**
  67. * set inputFrame which is used to record student information
  68. */
  69. inputFrame = new Frame();
  70. inputFrame.addWindowListener(new WindowAdapter() {
  71. public void windowClosing(WindowEvent e) {
  72. inputFrame.setVisible(false);
  73. }
  74. });
  75.  
  76. p1 = new Panel(new GridLayout(9, 2));
  77. p2 = new Panel();
  78. String lbname[] = {"Code:", "Name:", "Sex:", "BirthPlace:",
  79. "Class:", "Chinese:", "Math:", "English:", "TotalScore:"};
  80. String btnname[] = {"Save", "Delete", " Exit "};
  81. for(int i=0; i<9; i++) {
  82. lb[i] = new Label(lbname[i]);
  83. tf[i] = new TextField(15);
  84. p1.add(lb[i]);
  85. p1.add(tf[i]);
  86. }
  87. for(int i=0; i<3; i++) {
  88. btn[i] = new Button(btnname[i]);
  89. btn[i].addActionListener(this);
  90. p2.add(btn[i]);
  91. }
  92. btn[2].setActionCommand("input");
  93. inputFrame.add(p1, BorderLayout.CENTER);
  94. inputFrame.add(p2, BorderLayout.SOUTH);
  95.  
  96. inputFrame.pack();
  97. inputFrame.setLocationRelativeTo(mainFrame);
  98.  
  99. /**
  100. * set searchFrame which is used to search student information
  101. */
  102. searchFrame = new Frame("Search student");
  103. searchFrame.addWindowListener(new WindowAdapter() {
  104. public void windowClosing(WindowEvent e) {
  105. searchFrame.setVisible(false);
  106. }
  107. });
  108.  
  109. p3 = new Panel(new GridLayout(2, 2));
  110. p4 = new Panel();
  111. String lbname2[] = {"Code:", "Name:"};
  112. String btnname2[] = {"Search", " Exit "};
  113. for(int i=0; i<2; i++) {
  114. lb2[i] = new Label(lbname2[i]);
  115. tf2[i] = new TextField(15);
  116. p3.add(lb2[i]);
  117. p3.add(tf2[i]);
  118. }
  119. for(int i=0; i<2; i++) {
  120. btn2[i] = new Button(btnname2[i]);
  121. btn2[i].addActionListener(this);
  122. p4.add(btn2[i]);
  123. }
  124. btn2[1].setActionCommand("search");
  125. searchFrame.add(p3, BorderLayout.CENTER);
  126. searchFrame.add(p4, BorderLayout.SOUTH);
  127.  
  128. searchFrame.pack();
  129. searchFrame.setLocationRelativeTo(mainFrame);
  130.  
  131. /**
  132. * IO operation object
  133. */
  134. ioo = new IOOperation();
  135. student = ioo.getAllStudent();
  136.  
  137. }
  138.  
  139. public void actionPerformed(ActionEvent e) {
  140. /**
  141. * MenuItem action
  142. */
  143. if (e.getSource() instanceof MenuItem) {
  144. MenuItem mi = (MenuItem) e.getSource();
  145. if (mi.getLabel().equals("Record")) {
  146. inputFrame.setTitle("Record");
  147. for(int i=0; i<9; i++)
  148. tf[i].setText("");
  149. p2.remove(btn[1]);
  150. btn[0].setActionCommand("input");
  151. inputFrame.setVisible(true);
  152. }
  153. else if (mi.getLabel().equals("Search")) {
  154. searchFrame.setVisible(true);
  155. }
  156. else if (mi.getLabel().equals("Modify")) {
  157. inputFrame.setTitle("Modify");
  158. if(stu != null) {
  159. tf[0].setText(stu.getCode());
  160. tf[1].setText(stu.getName());
  161. tf[2].setText(stu.getSex());
  162. tf[3].setText(stu.getBirthPlace());
  163. tf[4].setText(stu.getStuClass());
  164. tf[5].setText(stu.getChinese() + "");
  165. tf[6].setText(stu.getMath() + "");
  166. tf[7].setText(stu.getEnglish() + "");
  167. tf[8].setText(stu.getTotalScore() + "");
  168. }
  169. p2.remove(btn[2]);
  170. p2.add(btn[1]);
  171. p2.add(btn[2]);
  172. btn[0].setActionCommand("modify");
  173. inputFrame.setVisible(true);
  174. }
  175. else if (mi.getLabel().equals("Exit"))
  176. System.exit(0);
  177. else if (mi.getLabel().equals("About")) {
  178. final Dialog progInfo = new Dialog(mainFrame, "ProgInfo", true);
  179. progInfo.addWindowListener(new WindowAdapter() {
  180. public void windowClosing(WindowEvent e) {
  181. progInfo.dispose();
  182. }
  183. });
  184. progInfo.setLayout(new FlowLayout());
  185. Label l = new Label("Student Information System");
  186. progInfo.add(l);
  187. progInfo.setSize(200, 80);
  188. progInfo.setLocationRelativeTo(mainFrame);
  189. progInfo.setVisible(true);
  190. }
  191. }
  192. /**
  193. * Button action
  194. */
  195. else {
  196. Button btn = (Button)e.getSource();
  197. if(btn.getLabel().equals("Save")) {
  198. if(!tf[0].getText().equals("") &&
  199. !tf[1].getText().equals("") &&
  200. !tf[2].getText().equals("") &&
  201. !tf[3].getText().equals("") &&
  202. !tf[4].getText().equals("") &&
  203. !tf[5].getText().equals("") &&
  204. !tf[6].getText().equals("") &&
  205. !tf[7].getText().equals("") &&
  206. !tf[8].getText().equals("")) {
  207. Student s = new Student(tf[0].getText(), tf[1].getText(),
  208. tf[2].getText(), tf[3].getText(), tf[4].getText(),
  209. Integer.parseInt(tf[5].getText()),
  210. Integer.parseInt(tf[6].getText()),
  211. Integer.parseInt(tf[7].getText()),
  212. Integer.parseInt(tf[8].getText()));
  213. if(btn.getActionCommand().equals("input")) {
  214. for(int i=0; i<student.length; i++) {
  215. if(student[i] == null) {
  216. student[i] = s;
  217. break;
  218. }
  219. }
  220. ioo.write(student);
  221. }
  222. else {
  223. for(int i=0; i<student.length; i++) {
  224. if(student[i].equals(stu)) {
  225. student[i] = s;
  226. break;
  227. }
  228. }
  229. ioo.write(student);
  230. }
  231. }
  232. inputFrame.setVisible(false);
  233. stuInfo.setText("");
  234. }
  235. else if(btn.getLabel().equals("Delete")) {
  236. int index = 200;
  237. if(stu != null) {
  238. for(int i=0; i<student.length; i++) {
  239. if(student[i]!=null && student[i].equals(stu)) {
  240. index = i;
  241. if(i != student.length-1)
  242. student[i] = student[i+1];
  243. else
  244. student[i] = null;
  245. }
  246. if(i==index && student[i+1]==null)
  247. break;
  248. else if(i>index && i<student.length-1) {
  249. student[i] = student[i+1];
  250. if(i == student.length - 1)
  251. student[i] = null;
  252. }
  253. }
  254. ioo.write(student);
  255. }
  256. stu = null;
  257. inputFrame.setVisible(false);
  258. stuInfo.setText("");
  259. /*for(int i=0; i<student.length; i++) {
  260. if(student[i]!=null)
  261. System.out.println(i + " " + student[i].getCode());
  262. }*/
  263. }
  264. else if(btn.getLabel().equals("Search")) {
  265. stu = null;
  266. if(!tf2[0].getText().equals("") ||
  267. !tf2[1].getText().equals("")) {
  268. String condition = "";
  269. if(!tf2[0].getText().equals("")) {
  270. condition = tf2[0].getText();
  271. }
  272. else
  273. condition = tf2[1].getText();
  274. for(int i=0; i<student.length; i++) {
  275. if (student[i] != null) {
  276. if (student[i].getCode().equals(condition) ||
  277. student[i].getName().equals(condition)) {
  278. stu = student[i];
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. if(stu != null) {
  285. stuInfo.setText("Code: " + stu.getCode() + "\n" +
  286. "Name: " + stu.getName() + "\n" +
  287. "Sex: " + stu.getSex() + "\n" +
  288. "BirthPlace: " + stu.getBirthPlace() + "\n" +
  289. "Class: " + stu.getStuClass() + "\n" +
  290. "Chinese: " + stu.getChinese() + "\n" +
  291. "Math: " + stu.getMath() + "\n" +
  292. "English: " + stu.getEnglish() + "\n" +
  293. "TotalScore: " + stu.getTotalScore());
  294. }
  295. searchFrame.setVisible(false);
  296. }
  297. else if(btn.getLabel().equals(" Exit "))
  298. if(btn.getActionCommand().equals("input"))
  299. inputFrame.setVisible(false);
  300. else
  301. searchFrame.setVisible(false);
  302. }
  303. }
  304.  
  305. public static void main(String[] args) {
  306. new StudentInformation();
  307. }
  308. }
  309.  
  310. /**
  311. * class which is used to store student information
  312. */
  313. class Student implements Serializable {
  314. private String code;
  315. private String name;
  316. private String sex;
  317. private String birthPlace;
  318. private String stuClass;
  319. private int Chinese;
  320. private int Math;
  321. private int English;
  322. private int totalScore;
  323.  
  324. public Student(String code, String name, String sex, String birthPlace,
  325. String stuClass, int chinese, int math, int english, int totalScore) {
  326. super();
  327. this.code = code;
  328. this.name = name;
  329. this.sex = sex;
  330. this.birthPlace = birthPlace;
  331. this.stuClass = stuClass;
  332. Chinese = chinese;
  333. Math = math;
  334. English = english;
  335. this.totalScore = totalScore;
  336. }
  337.  
  338. public String getBirthPlace() {
  339. return birthPlace;
  340. }
  341.  
  342. public int getChinese() {
  343. return Chinese;
  344. }
  345.  
  346. public String getCode() {
  347. return code;
  348. }
  349.  
  350. public int getEnglish() {
  351. return English;
  352. }
  353.  
  354. public int getMath() {
  355. return Math;
  356. }
  357.  
  358. public String getName() {
  359. return name;
  360. }
  361.  
  362. public String getSex() {
  363. return sex;
  364. }
  365.  
  366. public String getStuClass() {
  367. return stuClass;
  368. }
  369.  
  370. public int getTotalScore() {
  371. return totalScore;
  372. }
  373.  
  374. public boolean equals(Object obj) {
  375. if(obj != null && (obj instanceof Student))
  376. if(this.getCode().equals(((Student)obj).getCode()) &&
  377. this.getName().equals(((Student)obj).getName()) &&
  378. this.getSex().equals(((Student)obj).getSex()) &&
  379. this.getBirthPlace().equals(((Student)obj).getBirthPlace()) &&
  380. this.getStuClass().equals(((Student)obj).getStuClass()) &&
  381. this.getChinese() == ((Student)obj).getChinese() &&
  382. this.getMath() == ((Student)obj).getMath() &&
  383. this.getEnglish() == ((Student)obj).getEnglish() &&
  384. this.getTotalScore() == ((Student)obj).getTotalScore())
  385. return true;
  386. return false;
  387. }
  388. }
  389.  
  390. /**
  391. * class which is used to do I/O operation
  392. */
  393. class IOOperation {
  394. private File file = new File("C:\\stuinfo.txt");
  395.  
  396. public IOOperation() {
  397. try {
  398. if(!file.exists())
  399. file.createNewFile();
  400. } catch (IOException e) {
  401. e.printStackTrace();
  402. }
  403. }
  404.  
  405. /**
  406. * write to file
  407. */
  408. public void write(Student[] s) {
  409. try {
  410. objOut.writeObject(s);
  411. objOut.close();
  412. fos.close();
  413. } catch(Exception e) {
  414. e.printStackTrace();
  415. }
  416. }
  417.  
  418. /**
  419. * read all student information from file
  420. */
  421. public Student[] getAllStudent() {
  422. Student ss[] = new Student[100];
  423. try {
  424. if (file.length() > 0) {
  425. ss = (Student[]) ois.readObject();
  426. ois.close();
  427. fis.close();
  428. }
  429. } catch(Exception e) {
  430. e.printStackTrace();
  431. }
  432. return ss;
  433. }
  434. }

回复 "学生信息"

这儿你可以回复上面这条便签

captcha