java实验5
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210519155828278.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JyeWNlX3pzcQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210519155848304.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JyeWNlX3pzcQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210519155911703.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JyeWNlX3pzcQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210519155930270.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JyeWNlX3pzcQ==,size_16,color_FFFFFF,t_70)
代码:
frame类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class frame {
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();
JLabel label1 = new JLabel("请输入文件路径:");
JTextField text1 = new JTextField(10);
JButton btn1 = new JButton("显示文件内容");
JButton save = new JButton("保存");
JButton sorted = new JButton("按照第1门成绩升序排序");
JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
JSplitPane splitPane;
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
JPanel panel = new JPanel();
// LinkedList<student> students;
student stu[] = new student[100];
void quickSort(student[] src, int begin, int end) {
if (begin < end) {
student key = src[begin];
int i = begin;
int j = end;
while (i < j) {
while (i < j && src[j].first_score > key.first_score) {
j--;
}
if (i < j) {
src[i] = src[j];
i++;
}
while (i < j && src[i].first_score < key.first_score) {
i++;
}
if (i < j) {
src[j] = src[i];
j--;
}
}
src[i] = key;
quickSort(src, begin, i - 1);
quickSort(src, i + 1, end);
}
}
int n;
void show(String s) throws IOException {
try {
BufferedReader bre = null;
String str;
bre = new BufferedReader(new FileReader(s));
while ((str = bre.readLine()) != null) {
area1.append(str + "\r\n");
}
bre.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
}
void writeStu(String s) {
String studentInfo = "";
File fp = new File(s);
try {
if (fp.exists()) {
BufferedReader br = new BufferedReader(new InputStreamReader((new FileInputStream(fp))));
String infodata = null;
while ((infodata = br.readLine()) != null) {
studentInfo += infodata;
studentInfo += "\n";
}
br.close();
String[] infoArr = studentInfo.split("\n");
for (int i = 0; i < infoArr.length; i++) {
String[] stuArr = infoArr[i].split(" ");
if (stuArr.length > 0) {
student st = new student(stuArr[0], Double.parseDouble(stuArr[1]),
Double.parseDouble(stuArr[2]));
// students.add(s);
stu[i] = st;
}
n = i;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
frame() {
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame1.dispose();
String path = text1.getText();// 获取地址
if (path.isEmpty())
System.out.println("未输入文件名");
else {
try {
show(path);
} catch (IOException e1) {
e1.printStackTrace();
}
}
frame2.setTitle(path);
frame2.setSize(300, 300);
frame2.setLocation(screenWidth / 4, screenHeight / 4);
JPanel panel2 = new JPanel();
panel2.add(save);
panel2.add(sorted);
panel2.setLayout(new GridLayout(1, 2, 5, 5));
frame2.add(panel2, BorderLayout.NORTH);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, area1, area2);
splitPane.setDividerLocation(120);
frame2.add(splitPane, BorderLayout.CENTER);
frame2.setVisible(true);
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = area1.getText();
try {
FileWriter fw = new FileWriter(text1.getText());
fw.write(s1);
fw.close();
area2.append("文本保存成功!" + "\n");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
sorted.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
area2.append("按第一门成绩排序(升序)之后,学生信息如下:" + "\n");
writeStu(text1.getText());
// sort(stu, n);
// C:\Users\ZPZ\Desktop\Home_Work\Java\Experiment5\src\data.txt
// student trmp = new student();
// n = n + 1;
quickSort(stu, 0, n);
System.out.println(n);
for (int i = 0; i < n + 1; i++) {
// System.out.println(stu[i].name + " " + stu[i].first_score + " " +
// stu[i].second_score + "\n");
area2.append(stu[i].name + " " + stu[i].first_score + " " + stu[i].second_score + "\n");
}
}
});
frame1.setSize(300, 300);
frame1.setLocation(screenWidth / 4, screenHeight / 4);
panel.setLayout(new GridLayout(3, 1, 5, 5));
panel.add(label1);
panel.add(text1);
panel.add(btn1);
frame1.add(panel);
frame1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 15));
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
public static void main(String[] args) {
new frame();
}
}
student类
public class student
{
String name;
double first_score;
double second_score;
student ()
{
this.name = null;
this.first_score = 0;
this.second_score = 0;
}
student(String name,double fs,double ss)
{
this.first_score = fs;
this.name = name;
this.second_score = ss;
}
public void setName(String name)
{
this.name = name;
}
public void setFirst_score(double first_score) {
this.first_score = first_score;
}
public void setSecond_score(double second_score) {
this.second_score = second_score;
}
}
效果图: