题目:
5. 使用 字符流和和GUI类 编程实现以下功能:
(1)设计图形化界面,至少包括文本类控件类。接收从键盘输入姓名、学号、成绩,并保存到文本文件中,重复进行。(2)从文件中读取各学生的成绩,并计算所有学生成绩的平均值、最大值和最小值,排序后输出到另一文本文件。
插件选用了Eclipse的windowbuilder,所以代码看起来好多,但大部分都是自动化生成的
文件的输入输出选用了RandomAccessFile类,详细教程:
https://blog.csdn.net/akon_vm/article/details/7429245
参考代码:
package scoreframe1;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.Timer;
import java.io.*;
public class Student1{//一定是ApplicationWindow!!!
private static Jframe frame;
private static JTextField textField,textField_1;
public static JTextField textField_2;
private static RandomAccessFile random1;
private static File file1;
public static int result1,average1;
private static JLabel lblNewLabel_4;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run(){
try{Student1 window = new Student1();
window.frame.setVisible(true);
}catch (Exception e) {
e.printStackTrace();}
}
});
}
public Student1(){
try {initialize();
}catch(Exception e){
e.printStackTrace();
}
}
private void initialize() throws Exception{
frame = new Jframe();
frame.setFont(new Font("Dialog", Font.BOLD, 18));
frame.setTitle("u5B66u751Fu6210u7EE9u7BA1u7406");
frame.setResizable(false);
frame.setBounds(100, 100, 700, 400);
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 18));
textField.setBounds(117, 70, 430, 38);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("u786Eu5B9A");
btnNewButton.addActionListener((inputfile1)->{
file1=new File(textField.getText());
try{textField.setEnabled(false);
btnNewButton.setEnabled(false);//只能输入一次文件名字
if(!file1.exists()){file1.createNewFile();//没有文件,创建新的
random1=new RandomAccessFile(file1,"rw");
random1.seek(file1.length());}//seek调整写入位置,实现追加写入
else{random1=new RandomAccessFile(file1,"rw");
random1.seek(file1.length());
}
}catch(IOException io_1){
io_1.printStackTrace();
}catch(Exception excp1){
excp1.printStackTrace();
}
});
btnNewButton.setFont(new Font("等线", Font.BOLD, 18));
btnNewButton.setBounds(566, 69, 109, 38);frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("u8F93u5165u6587u4EF6u540DuFF1A");
lblNewLabel.setFont(new Font("等线", Font.BOLD, 18));
lblNewLabel.setBounds(10, 69, 118, 38);frame.getContentPane().add(lblNewLabel);
lblNewLabel_4 = new JLabel("");
lblNewLabel_4.setFont(new Font("Consolas", Font.BOLD, 24));lblNewLabel_4.setBounds(442, 321, 254, 50);
frame.getContentPane().add(lblNewLabel_4);
showtime();
JLabel lblNewLabel_1 = new JLabel(" u5B66u751Fu6210u7EE9u7BA1u7406u5C0Fu7A0Bu5E8F");
lblNewLabel_1.setFont(new Font("等线", Font.BOLD, 29));
lblNewLabel_1.setBounds(117, 20, 430, 38);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("u8F93u5165u4FE1u606FuFF1A");
lblNewLabel_2.setFont(new Font("等线", Font.BOLD, 18));lblNewLabel_2.setBounds(10, 127, 118, 38);
frame.getContentPane().add(lblNewLabel_2);
textField_1 = new JTextField();
textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
textField_1.setToolTipText("u8BF7u6309u7167u683Cu5F0Fu8F93u5165uFF01uFF01uFF01");
textField_1.setColumns(10);textField_1.setBounds(117, 127, 430, 38);
frame.getContentPane().add(textField_1);
JLabel lblNewLabel_2_1 = new JLabel("u8BFBu53D6u6587u4EF6u540DuFF1A");
lblNewLabel_2_1.setFont(new Font("等线", Font.BOLD, 18));
lblNewLabel_2_1.setBounds(10, 187, 118, 38);
frame.getContentPane().add(lblNewLabel_2_1);
textField_2 = new JTextField();
textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
textField_2.setColumns(10);textField_2.setBounds(117, 187, 430, 38);
frame.getContentPane().add(textField_2);
JButton btnNewButton_1 = new JButton("u786Eu5B9A");
btnNewButton_1.addActionListener((infoevent1)->{
try{write();
}catch(Exception e){
e.printStackTrace();}
});
btnNewButton_1.setFont(new Font("等线", Font.BOLD, 18));
btnNewButton_1.setBounds(566, 127, 109, 38);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("u786Eu5B9A");
btnNewButton_2.addActionListener((read_event1)->{
try{new student_info().read();//
}catch(Exception e){
e.printStackTrace();}
});
btnNewButton_2.setFont(new Font("等线", Font.BOLD, 18));
btnNewButton_2.setBounds(566, 187, 109, 38);
frame.getContentPane().add(btnNewButton_2);
JLabel lblNewLabel_3 = new JLabel("stu management system 1.0.0");
lblNewLabel_3.setFont(new Font("Arial", Font.PLAIN, 18));
lblNewLabel_3.setBounds(0, 329, 264, 30);
frame.getContentPane().add(lblNewLabel_3);
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame.getContentPane());
JButton btnNewButton_2_1 = new JButton("u505Cu6B62");
btnNewButton_2_1.addActionListener((stopevent1)->{stop(); });
btnNewButton_2_1.setFont(new Font("等线", Font.BOLD, 18));
btnNewButton_2_1.setBounds(278, 243, 109, 38);
frame.getContentPane().add(btnNewButton_2_1);
}
private static void showtime(){
SimpleDateFormat format1=new SimpleDateFormat("hh:mm:ss");
try{
Timer time1=new Timer((1000),(timeevent1)->{
lblNewLabel_4.setText(" "+format1.format(new Date()));//每隔一秒刷新时间
Runtime.getRuntime().runFinalization();
});time1.start();
}catch(Exception excp_1){
excp_1.printStackTrace();}
}
private static void write() throws Exception{
try{random1.seek(file1.length());//追加写入
random1.write((textField_1.getText()+"rn").getBytes());//转换为byte[]有效地避免乱码!!!
}catch(Exception excp2){
excp2.printStackTrace();
}finally{textField_1.setText("");
Runtime.getRuntime().runFinalization();
}
}
private static void stop(){
try{textField_1.setEnabled(false);
random1.close();//停止写入,关闭RandomAccessFile文件流
}catch(IOException io_3){
JOptionPane.showConfirmDialog(null, "IO Exception!", "Oops", JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
io_3.printStackTrace();}
}
}
@SuppressWarnings("all")class student_info{
public static List scorelist=Collections.synchronizedList(new ArrayList<>());
public static List resultlist=Collections.synchronizedList(new ArrayList<>());
public void read() throws Exception{
try(BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(Student1.textField_2.getText())))){
String content=null;//String这种对象一定也要初始化!!!
while((content=br1.readLine())!=null){
String[] content1=content.split(" ");//分割,获取学生信息
if(content1.length==3){scorelist.add(new student(Integer.parseInt(content1[2])));}
}student.sort();new Confirm().show();
}
}
}
@SuppressWarnings("all")class student{
private int score;
public student(int score){
this.score=score;}
public int getscore(){
return score;}
public static void sort(){
try{
student_info.resultlist=student_info.scorelist.stream().sorted(new Comparator(){
@Override
public int compare(student o1, student o2) {
return o1.score-o2.score;
}
}).collect(Collectors.toList());
//有两种:①Collections.sort();②XXX.stream.sorted(new Comparator<>()).collect(XXX.toList);
for (Object o1:student_info.resultlist){
Student1.result1+=(int)o1;
}
Student1.average1=Student1.result1/student_info.resultlist.size();
}catch(Exception excp_n){
excp_n.printStackTrace();}
}
@Override
public String toString(){//对应的list的tostring(),本质上是调用对象类的tostring()方法,所以要重写。
return " "+this.score;}
}
class Confirm {
private Jframe frame;
private static JTextField textField;
private static RandomAccessFile file2;
public static void show(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {Confirm window = new Confirm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Confirm(){initialize();
}
private void initialize() {
frame = new Jframe();
frame.setBounds(300, 300, 500, 125);
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(72, 27, 279, 37);textField.setFont(new Font("宋体",Font.PLAIN,18));
textField.setColumns(10);frame.getContentPane().add(textField);
JButton btnNewButton = new JButton("u786Eu5B9A");
btnNewButton.setFont(new Font("宋体", Font.BOLD, 18));
btnNewButton.setBounds(369, 27, 107, 37);
btnNewButton.addActionListener((write_event1)->{
try{File file2_1=new File(textField.getText());
//RandomAccessFile计算最小最大值平均值,输出到指定文本,可以选用JFileChooser
file2_1.createNewFile();
file2=new RandomAccessFile(file2_1,"rw");
file2.seek(file2_1.length());
file2.writeBytes(student_info.resultlist.toString()+"rn"+"average:"+
student_info.resultlist.stream().mapToInt(student::getscore).average()+"rn"+"min value:"+student_info.resultlist.get(0).toString()+"rn"+"max value:"+student_info.resultlist.get(student_info.resultlist.size()-1).toString()+"rn");
file2.close();
}catch(Exception excp_n1){
excp_n1.printStackTrace();
}
});frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("u76EEu6807u8DEFu5F84:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
lblNewLabel.setBounds(0, 27, 74, 37);
frame.getContentPane().add(lblNewLabel);
}
} 


