求作业 编写一个图形界面的Java Application程序

2025-01-05 11:17:54
推荐回答(1个)
回答1:

/**
* 学生类
* @author Shurrik
*/
public class Student {
private String sid;
private String sname;
private int sage;
private char gender;
private String specialty;
private int grade;

public Student(String sid, String sname, int sage, char gender,
String specialty, int grade) {
super();
this.sid = sid;
this.sname = sname;
this.sage = sage;
this.gender = gender;
this.specialty = specialty;
this.grade = grade;
}

public String getSid() {
return sid;
}

public void setSid(String sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public int getSage() {
return sage;
}

public void setSage(int sage) {
this.sage = sage;
}

public char getGender() {
return gender;
}

public void setGender(char gender) {
this.gender = gender;
}

public String getSpecialty() {
return specialty;
}

public void setSpecialty(String specialty) {
this.specialty = specialty;
}

public int getGrade() {
return grade;
}

public void setGrade(int grade) {
this.grade = grade;
}

}

----------------------偶是分割线------------------------

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
* 学生管理者类
* @author Shurrik
*/
public class StudentManager {
private String filePath;
private Student stu;

public StudentManager(String filePath,Student stu) {
this.filePath = filePath;
this.stu = stu;

}

/**
* 按学号读取并返回学生信息
* @param sid 学号
* @return 学生信息
*/
public String loadStudentInfo(String sid) {
File StudentInfoFile = new File(this.filePath+"\\"+sid+".txt");
String StudentInfo = "";
try {
FileReader fr = new FileReader(StudentInfoFile);
BufferedReader reader = new BufferedReader(fr);
String line = "";
while((line = reader.readLine()) != null){
StudentInfo += line+"\r\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return StudentInfo;
}

/**
* 保存学生信息以文本文档的形式存入硬盘中
*/
public void saveStudentInfo() {
File StudentInfoFile = new File(this.filePath+"\\"+stu.getSid()+".txt");
try {
FileWriter fw = new FileWriter(StudentInfoFile);
BufferedWriter writer = new BufferedWriter(fw);
writer.write("学号:"+stu.getSid()+"\r\n");
writer.write("姓名:"+stu.getSname()+"\r\n");
writer.write("年龄:"+stu.getSage()+"\r\n");
writer.write("性别:"+stu.getGender()+"\r\n");
writer.write("专业:"+stu.getSpecialty()+"\r\n");
writer.write("成绩:"+stu.getGrade());
writer.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

----------------------偶是分割线------------------------

import java.io.File;
/**
* 测试类
* @author Shurrik
*/
public class Test {
public static void main(String[] args) {
Student stu = new Student("S2010001", "Jack", 21, '男', "Java", 94);
String filePath = "D:\\StudentInfo";
File folder = new File(filePath);
if(!folder.exists()){
folder.mkdirs();
}
StudentManager manager = new StudentManager(filePath, stu);
manager.saveStudentInfo();
String StudentInfo = manager.loadStudentInfo("S2010001");
System.out.println(StudentInfo);
}
}

基本满足了你的要求,其他功能很容易扩展,自己写写看吧。