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

题目:

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;/**Launch the application.*/
	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();}
			}
		});
	}/** Create the application.*/
	public Student1(){
		try {initialize();
		}catch(Exception e){
			e.printStackTrace();
		}
	}/** Initialize the contents of the frame.我用了nio和少许的io,大部分是windowbuilder自动生成代码*/
	private void initialize() throws Exception{
		frame = new JFrame();
		frame.setFont(new Font("Dialog", Font.BOLD, 18));
		frame.setTitle("\u5B66\u751F\u6210\u7EE9\u7BA1\u7406");
		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("\u786E\u5B9A");
		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();
			}
		});/*windowbuilder自动生成代码*/
		btnNewButton.setFont(new Font("等线", Font.BOLD, 18));
		btnNewButton.setBounds(566, 69, 109, 38);frame.getContentPane().add(btnNewButton);
		JLabel lblNewLabel = new JLabel("\u8F93\u5165\u6587\u4EF6\u540D\uFF1A");
		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("           \u5B66\u751F\u6210\u7EE9\u7BA1\u7406\u5C0F\u7A0B\u5E8F");
		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("\u8F93\u5165\u4FE1\u606F\uFF1A");
		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("\u8BF7\u6309\u7167\u683C\u5F0F\u8F93\u5165\uFF01\uFF01\uFF01");
		textField_1.setColumns(10);textField_1.setBounds(117, 127, 430, 38);
		frame.getContentPane().add(textField_1);
		JLabel lblNewLabel_2_1 = new JLabel("\u8BFB\u53D6\u6587\u4EF6\u540D\uFF1A");
		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("\u786E\u5B9A");
		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("\u786E\u5B9A");
		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("\u505C\u6B62");
		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);
	}/**利用javax.swing.Timer实现显示时间的时钟*/
	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()+"\r\n").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<student> scorelist=Collections.synchronizedList(new ArrayList<>());
	public static List<student> resultlist=Collections.synchronizedList(new ArrayList<>());
	/**用集合存入student对象,进行排序后输出到另一个集合*/
	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;}/**实现Comparator接口的匿名内部类,自定义排序方式*/
	public static void sort(){
		try{
			student_info.resultlist=student_info.scorelist.stream().sorted(new Comparator<student>(){
			@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;/**Launch the application.*/
	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();
				}
			}
		});
	}/**Create the application.*/
	public Confirm(){initialize();
	}/**Initialize the contents of the frame.*/
	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("\u786E\u5B9A");
		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()+"\r\n"+"average:"+
				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");
				file2.close();
			}catch(Exception excp_n1){
				excp_n1.printStackTrace();
			}
		});frame.getContentPane().add(btnNewButton);
		JLabel lblNewLabel = new JLabel("\u76EE\u6807\u8DEF\u5F84:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel.setBounds(0, 27, 74, 37);
		frame.getContentPane().add(lblNewLabel);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值