当前位置:   article > 正文

合工大路强java第四次作业第5题_rwbtn

rwbtn

题目:

5. 使用 字符流和和GUI类 编程实现以下功能:

(1)设计图形化界面,至少包括文本类控件类。接收从键盘输入姓名、学号、成绩,并保存到文本文件中,重复进行。(2)从文件中读取各学生的成绩,并计算所有学生成绩的平均值、最大值和最小值,排序后输出到另一文本文件。

插件选用了Eclipse的windowbuilder,所以代码看起来好多,但大部分都是自动化生成的

文件的输入输出选用了RandomAccessFile类,详细教程:

https://blog.csdn.net/akon_vm/article/details/7429245

参考代码:

  1. package scoreframe1;
  2. import java.text.SimpleDateFormat;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.util.*;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8. import javax.swing.Timer;
  9. import java.io.*;
  10. public class Student1{//一定是ApplicationWindow!!!
  11. private static JFrame frame;
  12. private static JTextField textField,textField_1;
  13. public static JTextField textField_2;
  14. private static RandomAccessFile random1;
  15. private static File file1;
  16. public static int result1,average1;
  17. private static JLabel lblNewLabel_4;/**Launch the application.*/
  18. public static void main(String[] args) {
  19. EventQueue.invokeLater(new Runnable() {
  20. public void run(){
  21. try{Student1 window = new Student1();
  22. window.frame.setVisible(true);
  23. }catch (Exception e) {
  24. e.printStackTrace();}
  25. }
  26. });
  27. }/** Create the application.*/
  28. public Student1(){
  29. try {initialize();
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. }
  33. }/** Initialize the contents of the frame.我用了nio和少许的io,大部分是windowbuilder自动生成代码*/
  34. private void initialize() throws Exception{
  35. frame = new JFrame();
  36. frame.setFont(new Font("Dialog", Font.BOLD, 18));
  37. frame.setTitle("\u5B66\u751F\u6210\u7EE9\u7BA1\u7406");
  38. frame.setResizable(false);
  39. frame.setBounds(100, 100, 700, 400);
  40. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. frame.getContentPane().setLayout(null);
  42. textField = new JTextField();
  43. textField.setFont(new Font("宋体", Font.PLAIN, 18));
  44. textField.setBounds(117, 70, 430, 38);
  45. frame.getContentPane().add(textField);
  46. textField.setColumns(10);
  47. JButton btnNewButton = new JButton("\u786E\u5B9A");
  48. btnNewButton.addActionListener((inputfile1)->{
  49. file1=new File(textField.getText());
  50. try{textField.setEnabled(false);
  51. btnNewButton.setEnabled(false);//只能输入一次文件名字
  52. if(!file1.exists()){file1.createNewFile();//没有文件,创建新的
  53. random1=new RandomAccessFile(file1,"rw");
  54. random1.seek(file1.length());}//seek调整写入位置,实现追加写入
  55. else{random1=new RandomAccessFile(file1,"rw");
  56. random1.seek(file1.length());
  57. }
  58. }catch(IOException io_1){
  59. io_1.printStackTrace();
  60. }catch(Exception excp1){
  61. excp1.printStackTrace();
  62. }
  63. });/*windowbuilder自动生成代码*/
  64. btnNewButton.setFont(new Font("等线", Font.BOLD, 18));
  65. btnNewButton.setBounds(566, 69, 109, 38);frame.getContentPane().add(btnNewButton);
  66. JLabel lblNewLabel = new JLabel("\u8F93\u5165\u6587\u4EF6\u540D\uFF1A");
  67. lblNewLabel.setFont(new Font("等线", Font.BOLD, 18));
  68. lblNewLabel.setBounds(10, 69, 118, 38);frame.getContentPane().add(lblNewLabel);
  69. lblNewLabel_4 = new JLabel("");
  70. lblNewLabel_4.setFont(new Font("Consolas", Font.BOLD, 24));lblNewLabel_4.setBounds(442, 321, 254, 50);
  71. frame.getContentPane().add(lblNewLabel_4);
  72. showtime();
  73. JLabel lblNewLabel_1 = new JLabel(" \u5B66\u751F\u6210\u7EE9\u7BA1\u7406\u5C0F\u7A0B\u5E8F");
  74. lblNewLabel_1.setFont(new Font("等线", Font.BOLD, 29));
  75. lblNewLabel_1.setBounds(117, 20, 430, 38);
  76. frame.getContentPane().add(lblNewLabel_1);
  77. JLabel lblNewLabel_2 = new JLabel("\u8F93\u5165\u4FE1\u606F\uFF1A");
  78. lblNewLabel_2.setFont(new Font("等线", Font.BOLD, 18));lblNewLabel_2.setBounds(10, 127, 118, 38);
  79. frame.getContentPane().add(lblNewLabel_2);
  80. textField_1 = new JTextField();
  81. textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
  82. textField_1.setToolTipText("\u8BF7\u6309\u7167\u683C\u5F0F\u8F93\u5165\uFF01\uFF01\uFF01");
  83. textField_1.setColumns(10);textField_1.setBounds(117, 127, 430, 38);
  84. frame.getContentPane().add(textField_1);
  85. JLabel lblNewLabel_2_1 = new JLabel("\u8BFB\u53D6\u6587\u4EF6\u540D\uFF1A");
  86. lblNewLabel_2_1.setFont(new Font("等线", Font.BOLD, 18));
  87. lblNewLabel_2_1.setBounds(10, 187, 118, 38);
  88. frame.getContentPane().add(lblNewLabel_2_1);
  89. textField_2 = new JTextField();
  90. textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
  91. textField_2.setColumns(10);textField_2.setBounds(117, 187, 430, 38);
  92. frame.getContentPane().add(textField_2);
  93. JButton btnNewButton_1 = new JButton("\u786E\u5B9A");
  94. btnNewButton_1.addActionListener((infoevent1)->{
  95. try{write();
  96. }catch(Exception e){
  97. e.printStackTrace();}
  98. });
  99. btnNewButton_1.setFont(new Font("等线", Font.BOLD, 18));
  100. btnNewButton_1.setBounds(566, 127, 109, 38);
  101. frame.getContentPane().add(btnNewButton_1);
  102. JButton btnNewButton_2 = new JButton("\u786E\u5B9A");
  103. btnNewButton_2.addActionListener((read_event1)->{
  104. try{new student_info().read();//
  105. }catch(Exception e){
  106. e.printStackTrace();}
  107. });
  108. btnNewButton_2.setFont(new Font("等线", Font.BOLD, 18));
  109. btnNewButton_2.setBounds(566, 187, 109, 38);
  110. frame.getContentPane().add(btnNewButton_2);
  111. JLabel lblNewLabel_3 = new JLabel("stu management system 1.0.0");
  112. lblNewLabel_3.setFont(new Font("Arial", Font.PLAIN, 18));
  113. lblNewLabel_3.setBounds(0, 329, 264, 30);
  114. frame.getContentPane().add(lblNewLabel_3);
  115. UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  116. SwingUtilities.updateComponentTreeUI(frame.getContentPane());
  117. JButton btnNewButton_2_1 = new JButton("\u505C\u6B62");
  118. btnNewButton_2_1.addActionListener((stopevent1)->{stop(); });
  119. btnNewButton_2_1.setFont(new Font("等线", Font.BOLD, 18));
  120. btnNewButton_2_1.setBounds(278, 243, 109, 38);
  121. frame.getContentPane().add(btnNewButton_2_1);
  122. }/**利用javax.swing.Timer实现显示时间的时钟*/
  123. private static void showtime(){
  124. SimpleDateFormat format1=new SimpleDateFormat("hh:mm:ss");
  125. try{
  126. Timer time1=new Timer((1000),(timeevent1)->{
  127. lblNewLabel_4.setText(" "+format1.format(new Date()));//每隔一秒刷新时间
  128. Runtime.getRuntime().runFinalization();
  129. });time1.start();
  130. }catch(Exception excp_1){
  131. excp_1.printStackTrace();}
  132. }
  133. private static void write() throws Exception{
  134. try{random1.seek(file1.length());//追加写入
  135. random1.write((textField_1.getText()+"\r\n").getBytes());//转换为byte[]有效地避免乱码!!!
  136. }catch(Exception excp2){
  137. excp2.printStackTrace();
  138. }finally{textField_1.setText("");
  139. Runtime.getRuntime().runFinalization();
  140. }
  141. }
  142. private static void stop(){
  143. try{textField_1.setEnabled(false);
  144. random1.close();//停止写入,关闭RandomAccessFile文件流
  145. }catch(IOException io_3){
  146. JOptionPane.showConfirmDialog(null, "IO Exception!", "Oops", JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
  147. io_3.printStackTrace();}
  148. }
  149. }
  150. @SuppressWarnings("all")class student_info{
  151. public static List<student> scorelist=Collections.synchronizedList(new ArrayList<>());
  152. public static List<student> resultlist=Collections.synchronizedList(new ArrayList<>());
  153. /**用集合存入student对象,进行排序后输出到另一个集合*/
  154. public void read() throws Exception{
  155. try(BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(Student1.textField_2.getText())))){
  156. String content=null;//String这种对象一定也要初始化!!!
  157. while((content=br1.readLine())!=null){
  158. String[] content1=content.split(" ");//分割,获取学生信息
  159. if(content1.length==3){scorelist.add(new student(Integer.parseInt(content1[2])));}
  160. }student.sort();new Confirm().show();
  161. }
  162. }
  163. }
  164. @SuppressWarnings("all")class student{
  165. private int score;
  166. public student(int score){
  167. this.score=score;}
  168. public int getscore(){
  169. return score;}/**实现Comparator接口的匿名内部类,自定义排序方式*/
  170. public static void sort(){
  171. try{
  172. student_info.resultlist=student_info.scorelist.stream().sorted(new Comparator<student>(){
  173. @Override
  174. public int compare(student o1, student o2) {
  175. return o1.score-o2.score;
  176. }
  177. }).collect(Collectors.toList());
  178. //有两种:①Collections.sort();②XXX.stream.sorted(new Comparator<>()).collect(XXX.toList);
  179. for (Object o1:student_info.resultlist){
  180. Student1.result1+=(int)o1;
  181. }
  182. Student1.average1=Student1.result1/student_info.resultlist.size();
  183. }catch(Exception excp_n){
  184. excp_n.printStackTrace();}
  185. }
  186. @Override
  187. public String toString(){//对应的list的tostring(),本质上是调用对象类的tostring()方法,所以要重写。
  188. return " "+this.score;}
  189. }
  190. class Confirm {
  191. private JFrame frame;
  192. private static JTextField textField;
  193. private static RandomAccessFile file2;/**Launch the application.*/
  194. public static void show(){
  195. EventQueue.invokeLater(new Runnable() {
  196. public void run() {
  197. try {Confirm window = new Confirm();
  198. window.frame.setVisible(true);
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. });
  204. }/**Create the application.*/
  205. public Confirm(){initialize();
  206. }/**Initialize the contents of the frame.*/
  207. private void initialize() {
  208. frame = new JFrame();
  209. frame.setBounds(300, 300, 500, 125);
  210. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  211. frame.getContentPane().setLayout(null);
  212. textField = new JTextField();
  213. textField.setBounds(72, 27, 279, 37);textField.setFont(new Font("宋体",Font.PLAIN,18));
  214. textField.setColumns(10);frame.getContentPane().add(textField);
  215. JButton btnNewButton = new JButton("\u786E\u5B9A");
  216. btnNewButton.setFont(new Font("宋体", Font.BOLD, 18));
  217. btnNewButton.setBounds(369, 27, 107, 37);
  218. btnNewButton.addActionListener((write_event1)->{
  219. try{File file2_1=new File(textField.getText());
  220. //RandomAccessFile计算最小最大值平均值,输出到指定文本,可以选用JFileChooser
  221. file2_1.createNewFile();
  222. file2=new RandomAccessFile(file2_1,"rw");
  223. file2.seek(file2_1.length());
  224. file2.writeBytes(student_info.resultlist.toString()+"\r\n"+"average:"+
  225. student_info.resultlist.stream().mapToInt(student::getscore).average()+"\r\n"+"min value:"+student_info.resultlist.get(0).toString()+"\r\n"+"max value:"+student_info.resultlist.get(student_info.resultlist.size()-1).toString()+"\r\n");
  226. file2.close();
  227. }catch(Exception excp_n1){
  228. excp_n1.printStackTrace();
  229. }
  230. });frame.getContentPane().add(btnNewButton);
  231. JLabel lblNewLabel = new JLabel("\u76EE\u6807\u8DEF\u5F84:");
  232. lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
  233. lblNewLabel.setBounds(0, 27, 74, 37);
  234. frame.getContentPane().add(lblNewLabel);
  235. }
  236. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/901409
推荐阅读
相关标签
  

闽ICP备14008679号