用户类
package Message;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private String name;
private String password;
private String affirmPassword;
private Date birthday;
public User(String name, String password, String affirmPassword, Date birthday){
this.name=name;
this.password=password;
this.affirmPassword=affirmPassword;
this.birthday=birthday;
}
public User(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAffirmPassword() {
return affirmPassword;
}
public void setAffirmPassword(String affirmPassword) {
this.affirmPassword = affirmPassword;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String toString(){
return "User{"+
" 用户名=\""+name+"\""+
" 密码=\""+password+"\""+
" 生日="+birthday+" }";
}
}
学生类
package Message;
import java.io.Serializable;
public class Student implements Serializable {
String name; // 姓名
String studentNumber; // 学号
double language; // 语文
double math; // 数学
double English; // 英语
public Student() {
}
public Student(String name, String studentNumber, double language, double math, double english) {
this.name = name;
this.studentNumber = studentNumber;
this.language = language;
this.math = math;
English = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
public double getLanguage() {
return language;
}
public void setLanguage(double language) {
this.language = language;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return English;
}
public void setEnglish(double english) {
English = english;
}
public double sum(){
return language+math+English;
}
public String toString(){
return "Student{"+
" 姓名=\""+name+"\""+"\n"+
" 学号=\""+studentNumber+"\""+"\n"+
" 语文=\""+language+"\""+"\n"+
" 数学=\""+math+"\""+"\n"+
" 英语=\""+English+"\n"+
" }";
}
}
整合用户类和学生类内容
package domain;
import Message.Student;
import Message.User;
import java.text.ParseException;
import java.util.Date;
public class Conform {
public static User conUser(String name, String password, String affirmPassword, String birthday){
User user=new User();
user.setName(name);
user.setPassword(password);
user.setAffirmPassword(affirmPassword);
try {
user.setBirthday(DateUtils.stringToDate(birthday,"yyyy-MM-dd"));
} catch (ParseException e) {
e.printStackTrace();
}
return user;
}
public static Student conStudent(String name,String studentNumber,String language,String math,String English){
Student student=new Student();
student.setName(name);
student.setStudentNumber(studentNumber);
student.setLanguage(Integer.valueOf(language));
student.setMath(Integer.valueOf(math));
student.setEnglish(Integer.valueOf(English));
return student;
}
}
Date与String的转换
package domain;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
public static String dateToString(Date date, String format){
SimpleDateFormat sdf=new SimpleDateFormat(format);
String s=sdf.format(date);
return s;
}
public static Date stringToDate(String str,String format) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat(format);
Date date=sdf.parse(str);
return date;
}
}
?主体
package domain;
import Message.Student;
import Message.User;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class Subject {
ArrayList<User> alUser=new ArrayList<>(); // 用户集合
ArrayList<Student> alStudent=new ArrayList<>(); // 学生集合
boolean b=false;
public Subject()
{
if(fileNULL("User.txt")){
getUser();
}
if(fileNULL("Student.txt")){
getStudent();
}
logIn();
}
// 登录界面
public void logIn(){
JFrame jFrame=new JFrame("用户登录");
jFrame.setSize(400,300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
JPanel jPanel=new JPanel(null);
JLabel jLabel1=new JLabel("用户名");
jLabel1.setBounds(50,50,50,30);
JTextField jTextField=new JTextField(10);
jTextField.setBounds(100,50,150,30);
JLabel jLabel2=new JLabel("密码");
jLabel2.setBounds(50,100,50,30);
JPasswordField jPasswordField=new JPasswordField(10);
jPasswordField.setBounds(100,100,150,30);
JButton jButton1=new JButton("登录");
jButton1.setBounds(80,200,60,30);
JButton jButton2=new JButton("注册");
jButton2.setBounds(200,200,60,30);
JButton jButton3=new JButton("保存");
jButton3.setBounds(300,200,60,30);
jPanel.add(jLabel1);
jPanel.add(jTextField);
jPanel.add(jLabel2);
jPanel.add(jPasswordField);
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(alUser.size()!=0){
for (User u:alUser) {
if(u.getName().equals(jTextField.getText()) && u.getPassword().equals(jPasswordField.getText())){
JOptionPane.showMessageDialog(jFrame ,"登录成功","success",JOptionPane.WARNING_MESSAGE);
b=true;
jFrame.setVisible(false);
mainWindow();
}
}
if(!b){
JOptionPane.showMessageDialog(jFrame,"登录失败","error",JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(jFrame,"无用户请注册","error",JOptionPane.ERROR_MESSAGE);
}
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
register();
}
});
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setUser();
}
});
jFrame.setContentPane(jPanel);
jFrame.setVisible(true);
}
public void register(){
JFrame jf=new JFrame("用户注册");
jf.setSize(300,400);
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jf.setResizable(false);
JPanel jp=new JPanel(null);
JLabel jLabel1=new JLabel("用户名");
jLabel1.setBounds(50,50,50,30);
JTextField jTextField=new JTextField(10);
jTextField.setBounds(100,50,150,30);
JLabel jLabel2=new JLabel("密码");
jLabel2.setBounds(50,100,50,30);
JPasswordField jPasswordField1=new JPasswordField(10);
jPasswordField1.setBounds(100,100,150,30);
JLabel jLabel3=new JLabel("确认密码");
jLabel3.setBounds(50,150,50,30);
JPasswordField jPasswordField2=new JPasswordField(10);
jPasswordField2.setBounds(100,150,150,30);
JLabel jLabel4=new JLabel("生日");
jLabel4.setBounds(50,200,50,30);
JTextField jTextField1=new JTextField(10);
jTextField1.setBounds(100,200,150,30);
JButton jButton=new JButton("注册");
jButton.setBounds(110,300,80,30);
jp.add(jLabel1);
jp.add(jTextField);
jp.add(jLabel2);
jp.add(jLabel3);
jp.add(jPasswordField2);
jp.add(jPasswordField1);
jp.add(jLabel4);
jp.add(jTextField1);
jp.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jPasswordField1.getText().equals(jPasswordField2.getText())){
alUser.add(Conform.conUser(jTextField.getText(),jPasswordField1.getText(),
jPasswordField2.getText(),jTextField1.getText()));
JOptionPane.showMessageDialog(jf,"注册成功","success",JOptionPane.WARNING_MESSAGE);
jf.setVisible(false);
}
else {
JOptionPane.showMessageDialog(jf,"两次密码不同","error",JOptionPane.ERROR_MESSAGE);
}
}
});
jf.setContentPane(jp);
jf.setVisible(true);
}
public void setUser(){ // 将用户集合写进文件
ObjectOutputStream oos= null;
try {
oos = new ObjectOutputStream(new FileOutputStream("User.txt"));
oos.writeObject(alUser);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getUser(){ //读取文件中的用户集合
ObjectInputStream ois= null;
ArrayList<User> al=new ArrayList<>();
try {
ois = new ObjectInputStream(new FileInputStream("User.txt"));
} catch (IOException e) {
e.printStackTrace();
}
try {
al=(ArrayList<User>) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
for (User user:al) {
alUser.add(user);
}
}
public boolean fileNULL(String s){
File file=new File(s);
if(file.exists()&&file.length()==0){
return false;
}
return true;
}
// 主窗口
public void mainWindow(){
JFrame jf=new JFrame();
jf.setSize(400,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
JPanel jp=new JPanel(null);
JLabel jLabel1=new JLabel("学生信息管理系统");
jLabel1.setBounds(150,30,100,30);
JButton jb1=new JButton("添加");
jb1.setBounds(100,80,80,30);
JButton jb2=new JButton("删除");
jb2.setBounds(220,80,80,30);
JButton jb3=new JButton("修改");
jb3.setBounds(100,130,80,30);
JButton jb4=new JButton("查找");
jb4.setBounds(220,130,80,30);
JButton jb5=new JButton("表格");
jb5.setBounds(100,180,80,30);
JButton jb6=new JButton("保存");
jb6.setBounds(220,180,80,30);
jp.add(jLabel1);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jb1.addActionListener(new ActionListener() { // 添加
@Override
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
jb2.addActionListener(new ActionListener() { // 删除
@Override
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
jb3.addActionListener(new ActionListener() { // 修改
@Override
public void actionPerformed(ActionEvent e) {
alterStudent();
}
});
jb4.addActionListener(new ActionListener() { // 查找
@Override
public void actionPerformed(ActionEvent e) {
findStudent();
}
});
jb5.addActionListener(new ActionListener() { // 表格
@Override
public void actionPerformed(ActionEvent e) {
form();
}
});
jb6.addActionListener(new ActionListener() { // 保存
@Override
public void actionPerformed(ActionEvent e) {
setStudent();
}
});
jf.setContentPane(jp);
jf.setVisible(true);
}
public void add(Student s){ // 添加学生
alStudent.add(s);
}
public boolean delete(String studentNumber){ // 删除学生
if(find(studentNumber)==null){
return false;
}
else {
alStudent.remove(find(studentNumber));
return true;
}
}
public boolean alter(Student student){ // 修改
if(delete(student.getStudentNumber())){
add(student);
return true;
}
else {
return false;
}
}
public Student find(String studentNumber){ // 查找
for (Student s:alStudent) {
if(s.getStudentNumber().equals(studentNumber)){
return s;
}
}
return null;
}
public void addStudent(){ // 添加学生窗口
JFrame jf=new JFrame("添加学生");
jf.setSize(300,400);
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jf.setResizable(false);
JPanel jp=new JPanel(null);
JLabel jLabel1=new JLabel("姓名");
jLabel1.setBounds(50,50,50,30);
JTextField jTextField1=new JTextField(10);
jTextField1.setBounds(100,50,150,30);
JLabel jLabel2=new JLabel("学号");
jLabel2.setBounds(50,100,50,30);
JTextField jTextField2=new JTextField(10);
jTextField2.setBounds(100,100,150,30);
JLabel jLabel3=new JLabel("语文");
jLabel3.setBounds(50,150,50,30);
JTextField jTextField3=new JTextField(10);
jTextField3.setBounds(100,150,150,30);
JLabel jLabel4=new JLabel("数学");
jLabel4.setBounds(50,200,50,30);
JTextField jTextField4=new JTextField(10);
jTextField4.setBounds(100,200,150,30);
JLabel jLabel5=new JLabel("英语");
jLabel5.setBounds(50,250,50,30);
JTextField jTextField5=new JTextField(10);
jTextField5.setBounds(100,250,150,30);
JButton jButton=new JButton("添加");
jButton.setBounds(80,300,80,30);
jp.add(jLabel1);
jp.add(jLabel2);
jp.add(jLabel3);
jp.add(jLabel4);
jp.add(jLabel5);
jp.add(jTextField1);
jp.add(jTextField2);
jp.add(jTextField3);
jp.add(jTextField4);
jp.add(jTextField5);
jp.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(Conform.conStudent(jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),
jTextField4.getText(),jTextField5.getText()));
JOptionPane.showMessageDialog(jf,"添加成功","success",JOptionPane.WARNING_MESSAGE);
}
});
jf.setContentPane(jp);
jf.setVisible(true);
}
public void alterStudent(){ // 修改学生信息窗口
JFrame jf=new JFrame("添加学生");
jf.setSize(300,400);
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jf.setResizable(false);
JPanel jp=new JPanel(null);
JLabel jLabel1=new JLabel("姓名");
jLabel1.setBounds(50,50,50,30);
JTextField jTextField1=new JTextField(10);
jTextField1.setBounds(100,50,150,30);
JLabel jLabel2=new JLabel("学号");
jLabel2.setBounds(50,100,50,30);
JTextField jTextField2=new JTextField(10);
jTextField2.setBounds(100,100,150,30);
JLabel jLabel3=new JLabel("语文");
jLabel3.setBounds(50,150,50,30);
JTextField jTextField3=new JTextField(10);
jTextField3.setBounds(100,150,150,30);
JLabel jLabel4=new JLabel("数学");
jLabel4.setBounds(50,200,50,30);
JTextField jTextField4=new JTextField(10);
jTextField4.setBounds(100,200,150,30);
JLabel jLabel5=new JLabel("英语");
jLabel5.setBounds(50,250,50,30);
JTextField jTextField5=new JTextField(10);
jTextField5.setBounds(100,250,150,30);
JButton jButton=new JButton("修改");
jButton.setBounds(80,300,80,30);
jp.add(jLabel1);
jp.add(jLabel2);
jp.add(jLabel3);
jp.add(jLabel4);
jp.add(jLabel5);
jp.add(jTextField1);
jp.add(jTextField2);
jp.add(jTextField3);
jp.add(jTextField4);
jp.add(jTextField5);
jp.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
alter(Conform.conStudent(jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),
jTextField4.getText(),jTextField5.getText()));
JOptionPane.showMessageDialog(jf,"修改成功","success",JOptionPane.WARNING_MESSAGE);
}
});
jf.setContentPane(jp);
jf.setVisible(true);
}
public void deleteStudent(){ // 删除学生窗口
JFrame jf=new JFrame("删除学生");
jf.setSize(300,250);
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jf.setResizable(false);
JPanel jp=new JPanel(null);
JLabel jLabel=new JLabel("学号");
jLabel.setBounds(50,100,50,30);
JTextField jTextField=new JTextField(10);
jTextField.setBounds(110,100,150,30);
JButton jb=new JButton("删除");
jb.setBounds(120,150,60,30);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
delete(jTextField.getText());
JOptionPane.showMessageDialog(jf,"删除成功","success",JOptionPane.WARNING_MESSAGE);
}
});
jp.add(jLabel);
jp.add(jTextField);
jp.add(jb);
jf.setContentPane(jp);
jf.setVisible(true);
}
public void findStudent(){ // 查找学生窗口
JFrame jf=new JFrame("查找学生");
jf.setSize(400,400);
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
jf.setResizable(false);
JPanel jp=new JPanel(null);
JLabel jLabel=new JLabel("学号");
jLabel.setBounds(20,20,50,30);
JTextField jTextField=new JTextField(10);
jTextField.setBounds(80,20,150,30);
JButton jb=new JButton("查找");
jb.setBounds(20,70,60,30);
final JTextArea ja=new JTextArea(5,10);
ja.setBounds(100,70,200,100);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(find(jTextField.getText())!=null){
ja.append(find(jTextField.getText()).toString());
}
else {
JOptionPane.showMessageDialog(jf,"无该学生","success",JOptionPane.WARNING_MESSAGE);
}
}
});
jp.add(jLabel);
jp.add(jTextField);
jp.add(ja);
jp.add(jb);
jf.setContentPane(jp);
jf.setVisible(true);
}
public void setStudent(){ // 将学生集合写进文件
ObjectOutputStream oos= null;
try {
oos = new ObjectOutputStream(new FileOutputStream("Student.txt"));
oos.writeObject(alStudent);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getStudent(){ //读取文件中的学生集合
ObjectInputStream ois= null;
ArrayList<Student> al=new ArrayList<>();
try {
ois = new ObjectInputStream(new FileInputStream("Student.txt"));
} catch (IOException e) {
e.printStackTrace();
}
try {
al=(ArrayList<Student>) ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
for (Student s:al) {
alStudent.add(s);
}
}
public void form(){
JFrame jf=new JFrame("表格");
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
JPanel jp=new JPanel(new BorderLayout());
Object[] columnNames={"学号","姓名","语文","数学","英语","总分"};
Object[][] rowData=new Object[alStudent.size()][6];
for(int i=0;i<alStudent.size();i++){
rowData[i][0]=alStudent.get(i).getStudentNumber();
rowData[i][1]=alStudent.get(i).getName();
rowData[i][2]=alStudent.get(i).getLanguage();
rowData[i][3]=alStudent.get(i).getMath();
rowData[i][4]=alStudent.get(i).getEnglish();
rowData[i][5]=alStudent.get(i).sum();
}
TableModel tableModel=new DefaultTableModel(rowData,columnNames);
JTable table=new JTable(tableModel);
RowSorter<TableModel> rowSorter=new TableRowSorter<TableModel>(tableModel);
table.setRowSorter(rowSorter);
jp.add(table.getTableHeader(),BorderLayout.NORTH);
jp.add(table,BorderLayout.CENTER);
jf.setContentPane(jp);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
}
测试
package Test;
import Message.Student;
import domain.Subject;
public class Test {
public static void main(String[] args) {
new Subject();
}
}
运行结果
?
源代码链接:链接:https://pan.baidu.com/s/13WiRM491r0rVeqNIrWyK5Q? 提取码:abcd
?
?
|