GradeSystemPanel.java
Upload User: jt0027
Upload Date: 2011-09-11
Package Size: 3k
Code Size: 8k
Development Platform:

WINDOWS

  1. /**
  2.  * <p>Title:Student Grade System</p>
  3.  * <p>Discrition:</p>
  4.  * <p>Copyright: li168feng@yahoo.com.cn Copyright (c) 2003.1.3</p>
  5.  * <p>Company:</p>
  6.  *@autor lifeng
  7.  *@version 1.0  
  8.  */
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13. class GradeSystemPanel extends JPanel
  14. {
  15. private JLabel Title;//show the title
  16. private JPanel LayPanel;//Layout the title pane
  17. private JLabel NamLabel;//show name message
  18. private JTextField NamText;//input name text field
  19. private JLabel NumLabel;//show number message
  20. private JTextField NumText;//input number text field
  21. private JLabel GradLabel;//show grade message
  22. private JTextField GradText;//input grade text field
  23. private JTextArea ShowArea;//show the message area
  24. private JTextArea ResuArea;//show the result area
  25. private JPanel BtnPanel;//layout the button pane
  26. private JButton AddBtn;//add button
  27. private JButton DelBtn;//delete button
  28. private JButton SeaBtn;//search button
  29. private JButton ChangBtn;//change button
  30. private JButton CouBtn;//count the average and total grade button
  31. private JScrollPane ShowScrollPane;//a scroll pane on ShowArea
  32. private JScrollPane ResuScrollPane;//a scroll pane on ResuArea
  33. private int num = 0;//count the student number
  34. private String[][] StudMess= new String[10][10];//a temp array of student
  35. public GradeSystemPanel()
  36. {
  37. setLayout(new BorderLayout());
  38. //new a label for show the title
  39. Title = new JLabel("成绩系统",JLabel.CENTER);
  40. add(Title,BorderLayout.NORTH);
  41. //new a panel for lay out the name and number and grade
  42. LayPanel = new JPanel(new GridLayout(4,2));
  43. //new a label for show the name
  44. NamLabel = new JLabel("姓名");
  45. LayPanel.add(NamLabel);
  46. //new a text field for input the name
  47. NamText = new JTextField("",10);
  48. LayPanel.add(NamText);
  49. //new a label for show the number of student
  50. NumLabel = new JLabel("学号");
  51. LayPanel.add(NumLabel);
  52. //new a text field for input the number of student
  53. NumText = new JTextField("",10);
  54. LayPanel.add(NumText);
  55. //new a label of show the grade of student
  56. GradLabel = new JLabel("成绩");
  57. LayPanel.add(GradLabel);
  58. //new a text field for input the grade of student
  59. GradText = new JTextField("",10);
  60. LayPanel.add(GradText);
  61. //new a text area for show the all student message 
  62. ShowArea = new JTextArea();
  63. //new a scroll pane on the ShowArea
  64. ShowScrollPane = new JScrollPane(ShowArea);
  65. ShowArea.append(" name         number        graden");
  66. LayPanel.add(ShowScrollPane);
  67. //new a text area for show the search and average result
  68. ResuArea = new JTextArea();
  69. //new a scroll pane on ResuArea
  70. ResuScrollPane = new JScrollPane(ResuArea);
  71. LayPanel.add(ResuScrollPane);
  72. //add the panel
  73. add(LayPanel,BorderLayout.CENTER);
  74. //new a panel for lay out the button
  75. BtnPanel = new JPanel();
  76. //new a add button
  77. AddBtn = new JButton("添加");
  78. BtnPanel.add(AddBtn);
  79. //new a delete button
  80. DelBtn = new JButton("删除");
  81. BtnPanel.add(DelBtn);
  82. //new a search button
  83. SeaBtn = new JButton("查询");
  84. BtnPanel.add(SeaBtn);
  85. //new a change the student message button
  86. ChangBtn = new JButton("修改");
  87.     BtnPanel.add(ChangBtn);
  88. //new a cout the all student average button
  89. CouBtn = new JButton("算平均分");
  90. BtnPanel.add(CouBtn);
  91. add(BtnPanel,BorderLayout.SOUTH);
  92. //add the button action
  93. //first is addition button action event
  94. AddBtn.addActionListener(new ActionListener()
  95. {
  96. public void actionPerformed(ActionEvent event)
  97. {
  98. //add message function
  99. AddMessage();
  100. NamText.setText("");//clear the NameText
  101. NumText.setText("");//clear the NumText
  102. GradText.setText("");//clear the GradText
  103. }
  104. });
  105. //second is searching button action event
  106. SeaBtn.addActionListener(new ActionListener()
  107. {
  108. public void actionPerformed(ActionEvent event)
  109. {
  110. //clear the ResuArea
  111. ResuArea.setText("");
  112. //Searching message function
  113. SearchMess();
  114. }
  115. });
  116. //third is deleting button action event
  117. DelBtn.addActionListener(new ActionListener()
  118. {
  119. public void actionPerformed(ActionEvent event)
  120. {
  121. //deleting message funtion
  122. DeletMess();
  123. }
  124. });
  125. //four is changeing button action event
  126. ChangBtn.addActionListener(new ActionListener()
  127. {
  128. public void actionPerformed(ActionEvent evet)
  129. {
  130. //changing message function
  131. ChangeMess();
  132. }
  133. });
  134. //last is count the average button action event
  135. CouBtn.addActionListener(new ActionListener()
  136. {
  137. public void actionPerformed(ActionEvent event)
  138. {
  139. //clear the ResuArea 
  140. ResuArea.setText("");
  141. //Counting the average and total grade function
  142. CountAver();
  143. }
  144. });
  145. }
  146. //add the student message function
  147.     public void AddMessage()
  148. {
  149. String StrMess = new String();
  150. //add the student message 
  151. if(!NamText.getText().equals("")&&!NumText.getText().equals("")&&!GradText.getText().equals(""))
  152. {
  153. //add the message
  154. StrMess = NamText.getText()+"      "+NumText.getText()+"       "+GradText.getText()+"n";  
  155. StudMess[num][0] = NamText.getText();//add the name in StudMess
  156. StudMess[num][1] = NumText.getText();//add the number in StudMess
  157. StudMess[num][2] = GradText.getText();//add the grade in StudMess
  158. num++;
  159.         }
  160.         //show the message in show area
  161.         ShowArea.append(StrMess);
  162. }
  163. //search the student message function
  164. public void SearchMess()
  165. {
  166. //input searching name in this box
  167. String SeaNam = JOptionPane.showInputDialog("Input you want to search name?");
  168. String StrMess = new String();
  169. //search the searching name
  170. for(int i=0;i<num;i++)
  171. {
  172. if(SeaNam.equalsIgnoreCase(StudMess[i][0]))
  173.     StrMess = StudMess[i][0]+"       "+StudMess[i][1]+"       "+StudMess[i][2]+"n";
  174.     }
  175.     //show the searching name message in Result area
  176.     ResuArea.append(StrMess);
  177. }
  178. //delete the student message function
  179. public void DeletMess()
  180. {
  181. //input deleting name in this box
  182. String DeletNam = JOptionPane.showInputDialog("Input you want to delete name?");
  183. String StrMess = new String();
  184. //search the deleting name
  185. for (int i=0;i<num;i++)
  186. {
  187. if(DeletNam.equalsIgnoreCase(StudMess[i][0]))
  188. {
  189. for(int k=i;k<num-1;k++)
  190.     {
  191.      //move last number to prefer
  192.      StudMess[i][0]=StudMess[i+1][0];
  193.      StudMess[i][1]=StudMess[i+1][1];
  194.      StudMess[i][2]=StudMess[i+1][2];
  195.      StudMess[i][3]=StudMess[i+1][3];
  196.     }
  197.     //the number delete 1
  198.     num--;
  199. }
  200. }
  201. //clear the Show area
  202. ShowArea.setText("");
  203. //update new date
  204. ShowArea.append(" name         number           graden");
  205. for(int i=0;i<num;i++)
  206. {
  207. StrMess = StudMess[i][0]+"      "+StudMess[i][1]+"       "+StudMess[i][2]+"n";
  208.     //show the new message
  209.     ShowArea.append(StrMess);
  210. }
  211. }
  212. //change the student message function
  213. public void ChangeMess()
  214. {
  215. //input the changing name
  216. String ChangNam = JOptionPane.showInputDialog("Input you want to change naem?");
  217. for(int i=0;i<num;i++)
  218. {
  219. if(ChangNam.equalsIgnoreCase(StudMess[i][0]))
  220. {
  221. //input the new name
  222. String NewNam = JOptionPane.showInputDialog("Input the new name?");
  223. StudMess[i][0] = NewNam;
  224. //input the new number
  225. String NewNum = JOptionPane.showInputDialog("Input the new number?");
  226. StudMess[i][1] = NewNum;
  227. //input the new grade
  228. String NewGrad = JOptionPane.showInputDialog("Input the new grade?");
  229. StudMess[i][2] = NewGrad;
  230. }
  231. }
  232. String StrMess = new String();
  233. //clear the Show area
  234. ShowArea.setText("");
  235. //update new date
  236. ShowArea.append(" name         number           graden");
  237. for(int i=0;i<num;i++)
  238. {
  239. StrMess = StudMess[i][0]+"      "+StudMess[i][1]+"       "+StudMess[i][2]+"n";
  240.     //show the new message
  241.     ShowArea.append(StrMess);
  242. }
  243. }
  244. //count the student message function
  245. public void CountAver()
  246. {
  247. //init the total grade;
  248. int total = 0;
  249. //count the total grade
  250. for(int i=0;i<num;i++)
  251. {
  252. total +=Integer.parseInt(StudMess[i][2]);
  253. //show  the student number
  254. ResuArea.append("student total number is :"+num+"n");
  255. //show the all students' total grade 
  256. ResuArea.append("total grade is :"+total+"n");
  257. //show the all students' average grade
  258. ResuArea.append("average grade is :"+total/num+"n");
  259. }
  260. }