当前位置:   article > 正文

java学生管理系统(简易)(五)---查询学生(教师端)_简单的学生信息管理系统(含教师端和学生端)

简单的学生信息管理系统(含教师端和学生端)

查询界面,因为还不会数据库,嘤嘤嘤~就只能先这样写了

  1. package StudentMajor;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Set;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JPanel;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTable;
  13. import javax.swing.JTextField;
  14. public class SearchStudents {
  15. private JFrame jf;
  16. private JPanel jp,jp2,jp3;
  17. private JTextField jtf;
  18. private JButton jb,jb2,jb3;
  19. private JLabel jl;
  20. private JTable jtable;
  21. private JScrollPane jsp1,jsp2;
  22. private int countAStudentShow,countAllStudentShow;
  23. //用来计数查找名字显示或者不显示此处长度应该以学生的数量为准
  24. private Object[][] tableData = new Object[getFilelength()][];
  25. // // 定义一维数据作为列标题
  26. private final Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
  27. SearchStudents(){
  28. }
  29. //返回
  30. public static int getFilelength() {
  31. return StudentTools.getStudentsFromFile().toArray().length;
  32. }
  33. public void setTable() {
  34. // 定义二维数组作为表格数据
  35. for(int i=0;i< getFilelength();i++) {
  36. Object[] obj = new Object[]{"","","","","",""};
  37. tableData[i]=obj;
  38. }
  39. }
  40. public JScrollPane jspOfAllStudents() {
  41. //初始化表格的行列数
  42. setTable();
  43. //给表格中填入数据
  44. Set<StudentUser> set = StudentTools.getStudentsFromFile();
  45. if(set==null)
  46. return null;
  47. Object[] obj = set.toArray();
  48. // System.out.println(obj.length);
  49. for(int i=0;i<obj.length;i++) {
  50. StudentUser s = (StudentUser)obj[i];
  51. // System.out.println(s.getName());
  52. tableData[i][0] = s.getName();
  53. tableData[i][1] = s.getNum();
  54. tableData[i][2] = s.getScore1();
  55. tableData[i][3] = s.getScore2();
  56. tableData[i][4] = s.getScore3();
  57. tableData[i][5] = s.getSum();
  58. }
  59. jtable = new JTable(tableData,columnTitle);
  60. jtable.enable(false);//让这个表格不可编辑
  61. return new JScrollPane(jtable);
  62. }
  63. public JScrollPane jspOfAStudent(StudentUser s) {
  64. if(s==null)
  65. return null;
  66. // 定义二维数组作为表格数据
  67. Object[][] tableData =
  68. {
  69. new Object[]{"","","","","",""},
  70. };
  71. // 定义一维数据作为列标题
  72. Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
  73. //给表格中填入数据
  74. tableData[0][0] = s.getName();
  75. tableData[0][1] = s.getNum();
  76. tableData[0][2] = s.getScore1();
  77. tableData[0][3] = s.getScore2();
  78. tableData[0][4] = s.getScore3();
  79. tableData[0][5] = s.getSum();
  80. jtable = new JTable(tableData,columnTitle);
  81. jtable.enable(false);//让这个表格不可编辑
  82. return new JScrollPane(jtable);
  83. }
  84. public void init() {
  85. jf = new JFrame("学生管理系统");
  86. jf.setSize(300, 250);//窗体大小
  87. jf.setLocationRelativeTo(null);//居中显示
  88. jf.setLayout(new FlowLayout());//设置布局
  89. jf.setResizable(false); //设置窗口不能改变大小
  90. jp = new JPanel();
  91. jp2 = new JPanel();
  92. jp3 = new JPanel();
  93. jl = new JLabel("请输入学号:");
  94. jb = new JButton("查询");
  95. jb2 = new JButton("查询全部");
  96. jb3 = new JButton("返回");
  97. jtf = new JTextField(10);
  98. jp.add(jl);
  99. jp.add(jtf);
  100. jp.add(jb);
  101. jp2.add(jb2);
  102. jp3.add(jb3);
  103. jf.add(jp);
  104. jf.add(jp2);
  105. jf.add(jp3);
  106. //jf.add(jsp());
  107. //jf.pack();
  108. myEvent();
  109. jf.setVisible(true);
  110. }
  111. public void myEvent() {
  112. //(1)窗体关闭
  113. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  114. //(2)监听---确定
  115. jb.addActionListener(new ActionListener() {
  116. @Override
  117. public void actionPerformed(ActionEvent e) {
  118. if(countAStudentShow==0) {
  119. StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
  120. if(stu==null)
  121. JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
  122. else{
  123. jsp1 = jspOfAStudent(stu);
  124. jf.add(jsp1);
  125. jf.pack();
  126. countAStudentShow++;
  127. }
  128. }else {
  129. jf.remove(jsp1);
  130. StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
  131. if(stu==null)
  132. JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
  133. else {
  134. jsp1 = jspOfAStudent(stu);
  135. jf.add(jsp1);
  136. jf.pack();
  137. }
  138. }
  139. }
  140. });
  141. jb2.addActionListener(new ActionListener() {
  142. @Override
  143. public void actionPerformed(ActionEvent e) {
  144. if(countAllStudentShow==0) {
  145. if(jspOfAllStudents()==null)
  146. JOptionPane.showMessageDialog(null, "还没有学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
  147. else{
  148. jsp2 = jspOfAllStudents();
  149. jf.add(jsp2);
  150. jf.pack();
  151. countAllStudentShow++;
  152. }
  153. }else {
  154. jf.remove(jsp2);
  155. jsp2 = jspOfAllStudents();
  156. jf.add(jsp2);
  157. jf.pack();
  158. }
  159. }
  160. });
  161. jb3.addActionListener(new ActionListener() {
  162. @Override
  163. public void actionPerformed(ActionEvent e) {
  164. new TeacherUser().init();
  165. jf.dispose();
  166. }
  167. });
  168. }
  169. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/170196
推荐阅读
相关标签
  

闽ICP备14008679号