学生学籍管理系统(基于文件操作)
主要实现的功能
- 添加学生学籍
- 删除学生学籍
- 修改学生学籍
- 查看学生学籍
- 显示学生学籍(以表格形式显示)
- 对学生学号进行排序
- 登入检验,判断进入的是学生界面还是管理员界面
- 统计学生总人数
界面展示
用户
public class Account {
String id;
String password;
public Account(String id, String password){
this.id=id;
this.password=password;
}
}
学生信息
public class Student {
int id;
String name;
int age;
String sex;
String yx;
String p;
String zy;
String cla;
String bz;
public Student() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getYx() {
return yx;
}
public void setYx(String yx) {
this.yx = yx;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public String getZy() {
return zy;
}
public void setZy(String zy) {
this.zy = zy;
}
public String getCla() {
return cla;
}
public void setCla(String cla) {
this.cla = cla;
}
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
}
Database
public class Database {
//定义一个ArrayList容器,容器元素的类型定义为父类Item类型。
private ArrayList<Student> stu;
public Database(){
//只创建一个Arraylist对象。
stu = new ArrayList<Student>();
}
public int size(){
return this.stu.size();
}
public Student get(int index){
return this.stu.get(index);
}
/**
* 添加信息。
*/
public void addItem(Student theItem){
stu.add(theItem);
}
/**
* 修改信息。
*/
public void addItem(int index,Student item){
stu.set(index,item);
}
/**
* 删除信息。
*/
public Student delet(int index){
return stu.remove(index);
}
/**
* 列表显示当前所有CD和DVD的信息。
*/
}
登录界面
public class LoginGui{
private JFrame frame;
private JLabel user,password,title;//p4包括确认密码时的输入框;点击register按钮出现
private JTextField users;
private JTextField passwords;
private JButton log,cannel;
Boolean m1=false;
Boolean m2=false;
public LoginGui(){
makeframe();
}
public void makeframe(){
frame = new JFrame("欢迎使用");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
title=new JLabel("学生学籍管理系统");
title.setFont(new Font("楷体", Font.BOLD, 30));
title.setForeground(Color.CYAN);
title.setBounds(39, 20, 350, 30);
frame.add(title);
user = new JLabel("用户名");
user.setFont(new Font("微软雅黑", 0, 14));
user.setForeground(Color.BLACK);
user.setBounds(30, 80, 50, 30);
frame.add(user);
users = new JTextField(10);
users.setBounds(120, 80, 180, 25);
frame.add(users);
password = new JLabel("密码");
password.setFont(new Font("微软雅黑", 0, 14));
password.setForeground(Color.BLACK);
password.setBounds(30, 120, 50, 30);
frame.add(password);
passwords = new JPasswordField(10);
passwords.setBounds(120, 120, 180, 25);
frame.add(passwords);
log = new JButton("登录");
log.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
log.setBounds(20, 180, 130, 30);
frame.add(log);
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String id=users.getText();
String psw=passwords.getText();
int c=Integer.parseInt(psw);
int a=c%10;
FileReader fr = new FileReader("stu.txt");
BufferedReader br = new BufferedReader(fr);
String str=br.readLine();
while(str!=null) {
String regex="\\s";
String[] s=str.split(regex);
int b=Integer.parseInt(s[1]);
if(id.equals(s[0])&&a==b) {
MenuStu ms=new MenuStu();
frame.setVisible(false);
JOptionPane.showMessageDialog(null,"登入成功");
m1=true;
break;
}else if(id.equals(s[0])&&psw.equals(s[1])){
Menu menu=new Menu();
frame.setVisible(false);
JOptionPane.showMessageDialog(null,"登入成功");
m2=true;
break;
}
str=br.readLine();
}
if(m1&&m2) {
JOptionPane.showMessageDialog(frame,"查无此人");
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
cannel = new JButton("取消");
cannel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.BLUE, Color.black));
cannel.setBounds(190,180, 130, 30);
cannel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
frame.add(cannel);
ImageIcon img = new ImageIcon("11.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
frame.add(jl);
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("12.png"));
frame.setSize(350, 270);//设置大小
frame.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2-frame.getWidth()/2,d.height/2-frame.getHeight()/2);
}
}
学生菜单界面
public class MenuStu {
public JFrame mframe;
private JLabel mp,d,t;
private JButton add;
private JButton check;
private JButton Alter;
private JButton exit;
public MenuStu(){
makeframe();
}
public void makeframe(){
mframe = new JFrame(“菜单”);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mframe.setLayout(null);
mp=new JLabel("学生界面请选择操作项目");
mp.setFont(new Font("楷体", Font.BOLD, 20));
mp.setForeground(Color.CYAN);
mp.setBounds(220, 5, 350, 20);
mframe.add(mp);
add=new JButton("录入");
add.setBorder(BorderFactory.createRaisedBevelBorder());
add.setBounds(20, 50, 100, 30);
mframe.add(add);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Addstu();
}
});
check=new JButton("查询");
check.setBorder(BorderFactory.createRaisedBevelBorder());
check.setBounds(515, 50, 100, 30);
mframe.add(check);
check.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new FindStu();
}
});
Alter=new JButton("修改");
Alter.setBorder(BorderFactory.createRaisedBevelBorder());
Alter.setBounds(20, 120, 100, 30);
mframe.add(Alter);
Alter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new AlterStu();
}
});
Date currentTime;
SimpleDateFormat formatter;//进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
formatter=new SimpleDateFormat("yy-MM-dd HH:mm:ss");//时间格式
currentTime= new Date();//当前时间
String dateString=formatter.format(currentTime);
d=new JLabel("当前时间:");
d.setFont(new Font("楷体", Font.BOLD, 15));
d.setForeground(Color.BLACK);
d.setBounds(20, 300, 350, 15);
mframe.add(d);
t=new JLabel(dateString);
t.setFont(new Font("楷体", Font.BOLD, 15));
t.setForeground(Color.BLACK);
t.setBounds(100, 300, 350, 15);
mframe.add(t);
exit=new JButton("退出");
exit.setBorder(BorderFactory.createRaisedBevelBorder());
exit.setBounds(515, 300, 100, 30);
mframe.add(exit);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new LoginGui();
mframe.setVisible(false);//隐藏
JOptionPane.showMessageDialog(null,"成功退出");
}
});
ImageIcon img = new ImageIcon("11.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(0, -50,650 ,450);
mframe.add(jl);
mframe.setSize(650, 390);
mframe.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
mframe.setLocation(d.width/2-mframe.getWidth()/2,d.height/2-mframe.getHeight()/2);
}
}
管理员界面
public class Menu{
public JFrame mframe;
private JLabel mp,d,t;
private JButton add;
private JButton check;
private JButton delete;
private JButton Alter;
private JButton exit;
private JButton sum;
private JButton view;
public Menu(){
makeframe();
}
public void makeframe(){
mframe = new JFrame("菜单");
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mframe.setLayout(null);
mp=new JLabel("管理员界面请选择操作项目");
mp.setFont(new Font("楷体", Font.BOLD, 20));
mp.setForeground(Color.CYAN);
mp.setBounds(220, 5, 350, 20);
mframe.add(mp);
add=new JButton("录入");
add.setBorder(BorderFactory.createRaisedBevelBorder());
add.setBounds(20, 50, 100, 30);
mframe.add(add);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Addstu();
}
});
check=new JButton("查询");
check.setBorder(BorderFactory.createRaisedBevelBorder());
check.setBounds(515, 50, 100, 30);
mframe.add(check);
check.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new FindStu();
}
});
delete=new JButton("删除");
delete.setBorder(BorderFactory.createRaisedBevelBorder());
delete.setBounds(20, 120, 100, 30);
mframe.add(delete);
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new DelStu();
}
});
Alter=new JButton("修改");
Alter.setBorder(BorderFactory.createRaisedBevelBorder());
Alter.setBounds(515, 120, 100, 30);
mframe.add(Alter);
Alter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new AlterStu();
}
});
sum=new JButton("总人数");
sum.setBorder(BorderFactory.createRaisedBevelBorder());
sum.setBounds(20, 190, 100, 30);
mframe.add(sum);
sum.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Sum();
}
});
Date currentTime;
SimpleDateFormat formatter;//进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
formatter=new SimpleDateFormat("yy-MM-dd HH:mm:ss");//时间格式
currentTime= new Date();//当前时间
String dateString=formatter.format(currentTime);
d=new JLabel("当前时间:");
d.setFont(new Font("楷体", Font.BOLD, 15));
d.setForeground(Color.BLACK);
d.setBounds(20, 300, 350, 15);
mframe.add(d);
t=new JLabel(dateString);
t.setFont(new Font("楷体", Font.BOLD, 15));
t.setForeground(Color.BLACK);
t.setBounds(100, 300, 350, 15);
mframe.add(t);
view=new JButton("查看");
view.setBorder(BorderFactory.createRaisedBevelBorder());
view.setBounds(515, 190, 100, 30);
mframe.add(view);
view.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new View();
}
});
exit=new JButton("退出");
exit.setBorder(BorderFactory.createRaisedBevelBorder());
exit.setBounds(515, 300, 100, 30);
mframe.add(exit);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new LoginGui();
mframe.setVisible(false);//隐藏
JOptionPane.showMessageDialog(null,"成功退出");
}
});
ImageIcon img = new ImageIcon("11.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(0, -50,650 ,450);
mframe.add(jl);
mframe.setSize(650, 390);
mframe.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
mframe.setLocation(d.width/2-mframe.getWidth()/2,d.height/2-mframe.getHeight()/2);
}
}
添加学生学籍
public class Addstu{
private JFrame frame;
private JTextField idtext;
private JTextField nametext;
private JTextField agetext;
private JComboBox zytext;
private JTextField clatext;
private JComboBox settext;
private JComboBox ptext;
private JTextArea text;
private Writer fw;
private Writer fw1;
private BufferedReader br;
private FileReader fr;
String str1;
boolean check=false;
boolean n=false;
boolean b=false;
public Addstu(){
Date currentTime;
SimpleDateFormat formatter;//进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
formatter=new SimpleDateFormat("yyyy-MM-dd\\HH:mm:ss");//时间格式
currentTime= new Date();//当前时间
String dateString=formatter.format(currentTime);
makeframe();
JButton ok=new JButton("确定");
ok.setBounds(35, 320, 100, 25);
frame.add(ok);
JButton cancel=new JButton("取消");
cancel.setBounds(200, 320, 100, 25);
frame.add(cancel);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
String str=idtext.getText();
String strage=agetext.getText();
fr = new FileReader(StuFrame.file);
br = new BufferedReader(fr);
str1=br.readLine();
while(str1!=null) {
b=true;
String regex="\\s";
String regex1="201\\d{7}";
String regex2="\\d{1,2}";
String[] s=str1.split(regex);
if(str.matches(regex1)&&strage.matches(regex2)) {
if(str.equals(s[0])) {
JOptionPane.showMessageDialog(null,"学号相同,录入失败");
break;
}else {
try{
int a=Integer.parseInt(str)%10;
fw = new FileWriter(StuFrame.file,true);
fw.append(idtext.getText()+"\t"+
nametext.getText()+"\t"+
agetext.getText()+"\t"+
settext.getSelectedItem()+"\t"+
ptext.getSelectedItem()+"\t"+
zytext.getSelectedItem()+"\t"+
clatext.getText()+"\t"+
text.getText()+"\t"+
dateString+"\n");
fw1 = new FileWriter("stu.txt",true);
fw1.append(str+" "+a+"\n");
JOptionPane.showMessageDialog(frame,"录入成功");
check=true;
n=true;
}catch(Exception e1){
}finally {
try {
fw1.close();
fw.close();
} catch (IOException e1) {
System.out.println("错误");
}
}
}
if(check) {
str1=null;
}else {
str1=br.readLine();
}
}else {
JOptionPane.showMessageDialog(null,"学号输入有误");
break;
}
}
if(!b) {
String regex1="201\\d{7}";
String regex2="\\d{1,2}";
if(str.matches(regex1)) {
if(strage.matches(regex2)) {
int a=Integer.parseInt(str)%10;
fw = new FileWriter(StuFrame.file,true);
fw.append(idtext.getText()+"\t"+
nametext.getText()+"\t"+
agetext.getText()+"\t"+
settext.getSelectedItem()+"\t"+
ptext.getSelectedItem()+"\t"+
zytext.getSelectedItem()+"\t"+
clatext.getText()+"\t"+
text.getText()+"\t"+
dateString+"\n");
fw1 = new FileWriter("stu.txt",true);
fw1.append(str+" "+a+"\n");
JOptionPane.showMessageDialog(frame,"录入成功");
check=true;
}
}else {
JOptionPane.showMessageDialog(null,"学号输入有误");
}
}
}catch(Exception e1){
}finally {
try {
fw.close();
fw1.close();
br.close();
} catch (Exception e1) {
System.out.println("");
}
}
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
}
public static void main(String[] args) {
new Addstu();
}
public void makeframe() {
frame=new JFrame("添加信息");
frame.setLayout(null);
idtext=new JTextField();
idtext.setBounds(100, 20, 200, 25);
frame.add(idtext);
JLabel id=new JLabel();
id.setText("学号:");
id.setBounds(40, 18, 120, 30);
frame.add(id);
nametext=new JTextField(20);
nametext.setBounds(380, 20, 200, 25);
frame.add(nametext);
JLabel name=new JLabel();
name.setText("姓名:");
name.setBounds(330, 18, 120, 30);
frame.add(name);
agetext=new JTextField(20);
agetext.setBounds(100, 60, 200, 25);
frame.add(agetext);
JLabel age=new JLabel();
age.setText("年龄:");
age.setBounds(40, 60, 120, 30);
frame.add(age);
JLabel sex=new JLabel();
sex.setText("性别:");
sex.setBounds(330, 60, 120, 30);
frame.add(sex);
String[] ranks= {"男","女"};
settext=new JComboBox(ranks);
settext.setBounds(380, 60, 200, 25);
frame.add(settext);
JLabel p=new JLabel();
p.setText("政治面貌:");
p.setBounds(330, 100, 120, 30);
frame.add(p);
String[] ps= {"共青团员","党员","群众","其他"};
ptext=new JComboBox(ps);
ptext.setBounds(400, 100, 180, 25);
frame.add(ptext);
String[] zys= {"软件工程","网络工程","物联网工程","通信工程"};
zytext=new JComboBox(zys);
zytext.setBounds(100, 140, 200, 25);
frame.add(zytext);
JLabel zy=new JLabel();
zy.setText("专业:");
zy.setBounds(40, 140, 100, 30);
frame.add(zy);
clatext=new JTextField(20);
clatext.setBounds(380, 140, 200, 25);
frame.add(clatext);
JLabel cla=new JLabel();
cla.setText("班级:");
cla.setBounds(330, 140, 200, 30);
frame.add(cla);
text=new JTextArea();
text.setBounds(100, 180, 480, 100);
frame.add(text);
JLabel bz=new JLabel();
bz.setText("备注:");
bz.setBounds(40, 180, 100, 30);
frame.add(bz);
frame.setSize(650, 390);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
frame.setVisible(true);
}
}
删除学生学籍
public class DelStu {
private JFrame frame;
private JLabel title;//p4包括确认密码时的输入框;点击register按钮出现
private JTextField idt;
private JButton log,cancel;
private FileReader fr;
private BufferedReader br;
private Writer fw;
static String refile;
private boolean check=false;
public DelStu(){
makeframe();
refile=StuFrame.file+"1";
File users=new File(refile);
if(!users.exists()){
try{
users.createNewFile();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
}
public void makeframe(){
Date currentTime;
SimpleDateFormat formatter;//进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
formatter=new SimpleDateFormat("yyyy-MM-dd\\HH:mm:ss");//时间格式
currentTime= new Date();//当前时间
String dateString=formatter.format(currentTime);
frame = new JFrame("删除");
frame.setLayout(null);
title=new JLabel("输入删除的学号:");
title.setFont(new Font("楷体", Font.BOLD, 20));
title.setForeground(Color.BLACK);
title.setBounds(50, 40, 200, 20);
frame.add(title);
idt = new JTextField(10);
idt.setBounds(100, 100, 140, 25);
frame.add(idt);
log = new JButton("删除");
log.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
log.setBounds(105, 255, 130, 30);
frame.add(log);
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String id=idt.getText();
fr=new FileReader(StuFrame.file);
System.out.println(StuFrame.file);
br=new BufferedReader(fr);
fw = new FileWriter(refile,true);
String str=br.readLine();
while(str!=null) {
String regex="\\s";
String[] s=str.split(regex);
str=br.readLine();
if(id.equals(s[0])) {
check=true;
continue;
}
try{
fw.append(s[0]+"\t"+s[1]+"\t"+s[2]+"\t"+s[3]+"\t"
+s[4]+"\t"+s[5]+"\t"+s[6]+"\t"+s[7]+"\t"
+s[8]+"\n");
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"录入信息失败");
}finally {
// br.close();
// fr.close();
}
}
if(check) {
JOptionPane.showMessageDialog(frame,"删除成功");
frame.setVisible(false);
}else {
JOptionPane.showMessageDialog(frame,"查看死否有此人,删除失败");
frame.setVisible(false);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
fw.close();
fr.close();
br.close();
File file1=new File(StuFrame.file);
System.out.println(file1.delete());
File file2=new File("stulist.txt");
File file3=new File(refile);
System.out.println(file3.renameTo(file2));
// Addstu.file=refile;
// System.out.println(Addstu.file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
cancel = new JButton("返回");
cancel.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
cancel.setBounds(105, 300, 130, 30);
frame.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
frame.setSize(350, 400);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
new DelStu();
}
}
修改学生学籍
public class AlterStu {
private JFrame frame;
private JLabel title;
private JTextField idt;
private JButton log;
private FileReader fr;
private BufferedReader br;
private JTextField idtext;
private JTextField nametext;
private JTextField agetext;
private JTextField yxtext;
private JComboBox zytext;
private JTextField clatext;
private JComboBox settext;
private JComboBox ptext;
private JTextArea text;
private Writer fw;
private Writer fw1;
static String dfile;
int index=0;
int sum=0;
int c=0;
public AlterStu(){
makeframe();
dfile=StuFrame.file+"2";
}
public void makeframe(){
Date currentTime;
SimpleDateFormat formatter;//进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
formatter=new SimpleDateFormat("yyyy-MM-dd\\HH:mm:ss");//时间格式
currentTime= new Date();//当前时间
String dateString=formatter.format(currentTime);
frame = new JFrame("删除");
frame.setVisible(true);
frame.setLayout(null);
title=new JLabel("输入修改的学号:");
title.setForeground(Color.BLACK);
title.setBounds(150, 20, 200, 25);
frame.add(title);
idt = new JTextField(10);
idt.setBounds(255, 20, 140, 25);
frame.add(idt);
log = new JButton("search");
log.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
log.setBounds(400, 20, 80, 25);
frame.add(log);
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
String id=idt.getText();
fr=new FileReader(StuFrame.file);
System.out.println(StuFrame.file);
br=new BufferedReader(fr);
String str=br.readLine();
while(str!=null) {
String regex="\\s";
String[] s=str.split(regex);
if(id.equals(s[0])) {
idtext.setText(s[0]);
nametext.setText(s[1]);
agetext.setText(s[2]);
settext.setSelectedItem(s[3]);
ptext.setSelectedItem(s[4]);
zytext.setSelectedItem(s[5]);
clatext.setText(s[6]);
text.setText(s[7]);
}
index++;
str=br.readLine();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
fr.close();
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
JButton ok=new JButton("确定");
ok.setBounds(50, 325, 100, 25);
frame.add(ok);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
fw = new FileWriter(StuFrame.file,true);
System.out.println(StuFrame.file);
fw.append(idtext.getText()+"\t"+
nametext.getText()+"\t"+
agetext.getText()+"\t"+
settext.getSelectedItem()+"\t"+
ptext.getSelectedItem()+"\t"+
zytext.getSelectedItem()+"\t"+
clatext.getText()+"\t"+
text.getText()+"\t"+
dateString+"\n");
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"录入信息失败");
}finally {
try {
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
fr=new FileReader(StuFrame.file);
br=new BufferedReader(fr);
fw1 = new FileWriter(dfile,true);
String str=br.readLine();
while(str!=null) {
sum++;
String regex="\\s";
String[] s=str.split(regex);
str=br.readLine();
if(index==sum) {
continue;
}
try{
fw1.append(s[0]+"\t"+s[1]+"\t"+s[2]+"\t"+s[3]+"\t"+
s[4]+"\t"+s[5]+"\t"+s[6]+"\t"+s[7]+"\t"+dateString+"\n");
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"修改失败");
}finally {
}
}
// JOptionPane.showMessageDialog(null,"修改成功");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
fw1.close();
br.close();
fr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(frame,"修改成功");
File file1=new File(StuFrame.file);
System.out.println(file1.delete());
File file2=new File("stulist.txt");
File file3=new File(dfile);
System.out.println(dfile);
System.out.println(file3.renameTo(file2));
// Addstu.file=dfile;
}
});
JButton cancel=new JButton("取消");
cancel.setBounds(460, 325, 100, 25);
frame.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
idtext=new JTextField();
idtext.setBounds(100, 60, 200, 25);
frame.add(idtext);
JLabel id=new JLabel();
id.setText("学号:");
id.setBounds(40, 60, 120, 30);
frame.add(id);
nametext=new JTextField(20);
nametext.setBounds(380, 60, 200, 25);
frame.add(nametext);
JLabel name=new JLabel();
name.setText("姓名:");
name.setBounds(330, 60, 120, 30);
frame.add(name);
agetext=new JTextField(20);
agetext.setBounds(100, 100, 200, 25);
frame.add(agetext);
JLabel age=new JLabel();
age.setText("年龄:");
age.setBounds(40, 100, 120, 30);
frame.add(age);
JLabel sex=new JLabel();
sex.setText("性别:");
sex.setBounds(330, 100, 120, 30);
frame.add(sex);
String[] ranks= {"男","女"};
settext=new JComboBox(ranks);
settext.setBounds(380, 100, 200, 25);
frame.add(settext);
JLabel p=new JLabel();
p.setText("政治面貌:");
p.setBounds(330, 140, 120, 30);
frame.add(p);
String[] ps= {"共青团员","党员","群众","其他"};
ptext=new JComboBox(ps);
ptext.setBounds(400, 140, 180, 25);
frame.add(ptext);
String[] zys= {"软件工程","网络工程","物联网工程","通信工程"};
zytext=new JComboBox(zys);
zytext.setBounds(100, 180, 200, 25);
frame.add(zytext);
JLabel zy=new JLabel();
zy.setText("专业:");
zy.setBounds(40, 180, 100, 30);
frame.add(zy);
clatext=new JTextField(20);
clatext.setBounds(380, 180, 200, 25);
frame.add(clatext);
JLabel cla=new JLabel();
cla.setText("班级:");
cla.setBounds(330, 180, 200, 30);
frame.add(cla);
text=new JTextArea();
text.setBounds(100, 220, 480, 100);
frame.add(text);
JLabel bz=new JLabel();
bz.setText("备注:");
bz.setBounds(40, 220, 100, 30);
frame.add(bz);
frame.setSize(650, 390);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
}
public static void main(String[] args) {
new AlterStu();
}
}
查询学生学籍
public class FindStu {
private JFrame frame;
private JLabel title;
private JTextField idt;
private JTextArea find;
private JButton log,cancel;
private FileReader fr;
private BufferedReader br;
boolean check;
public FindStu(){
makeframe();
}
public void makeframe(){
frame = new JFrame("查找");
frame.setLayout(null);
title=new JLabel("输入学号:");
title.setFont(new Font("楷体", Font.BOLD, 20));
title.setForeground(Color.BLACK);
title.setBounds(28, 20, 150, 20);
frame.add(title);
idt = new JTextField(10);
idt.setBounds(140, 19, 140, 25);
frame.add(idt);
find=new JTextArea();
find.setBounds(10, 50, 315, 250);
frame.add(find);
log = new JButton("查找");
log.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
log.setBounds(25, 305, 130, 30);
frame.add(log);
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
int sum=0;
String id=idt.getText();
fr=new FileReader(StuFrame.file);
br=new BufferedReader(fr);
String str=br.readLine();
check=false;
while(str!=null) {
sum++;
String regex="\\s";
String[] s=str.split(regex);
if(id.equals(s[0])) {
String str1="学号:\t"+s[0]+"\n"+
"姓名:\t"+s[1]+"\n"+
"年龄:\t"+s[2]+"\n"+
"性别:\t"+s[3]+"\n"+
"政治面貌:\t"+s[4]+"\n"+
"专业:\t"+s[5]+"\n"+
"班级:\t"+s[6]+"\n"+
"备注:\\t"+s[7]+"\n"+
"添加的时间:"+s[8]+"\n";
find.setText(str1);
check=true;
}
str=br.readLine();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("还没有添加学生学籍");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(!check) {
JOptionPane.showMessageDialog(frame,"查无此人");
}
}
});
cancel = new JButton("返回");
cancel.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
cancel.setBounds(180, 305, 130, 30);
frame.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
frame.setSize(350, 400);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
frame.setVisible(true);
}
public static void main(String[] args) {
new FindStu();
}
}
显示学生学籍
public class View {
private JFrame frame;
public static Database db;
private JTextArea find;
private JButton cancel;
private FileReader fr;
private BufferedReader br;
private DefaultTableModel model;
private JTable table;
private JScrollPane scr;
public View(){
makeframe();
makeModel();
makeTable();
}
private void makeTable() {
makeModel();
this.table = new JTable(model);
this.scr = new JScrollPane(this.table);
}
public void makeframe(){
frame = new JFrame("查看");
frame.setLayout(null);
cancel = new JButton("返回");
cancel.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
cancel.setBounds(240, 305, 130, 30);
frame.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
frame.setSize(650, 390);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
frame.setVisible(true);
}
private void makeModel() {
String[] titles = { "学号", "姓名", "年龄", "性别","院系","政治面貌","专业","班级","备注" };
System.out.println(db.size());
Object[][] info = new Object[db.size()][titles.length];
for (int i = 0; i < db.size(); i++) {
Student data = db.get(i);
info[i][0] = data.getId();
info[i][1] = data.getName();
info[i][2] = data.getAge();
info[i][3] = data.getSex();
info[i][4] = data.getYx();
info[i][5] = data.getP();
info[i][6] = data.getZy();
info[i][7] = data.getCla();
info[i][8] = data.getBz();
}
this.model = new DefaultTableModel(info, titles);
}
public static void main(String[] args) {
new View();
}
}
查看学生总人数
public class Sum {
JFrame frame;
JLabel p,p1;
BufferedReader br;
FileReader fr;
int sum=0;
public Sum(){
makeframe();
}
public void makeframe(){
frame = new JFrame("总人数");
frame.setSize(200, 200);
frame.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2- frame.getHeight() / 2);
try {
fr = new FileReader(StuFrame.file);
br = new BufferedReader(fr);
String str1=br.readLine();
while(str1!=null) {
sum++;
str1=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
p=new JLabel("总人数:"+sum);
p.setFont(new Font("楷体", Font.BOLD, 20));
p.setForeground(Color.BLACK);
p.setBounds(220, 30, 350, 20);
frame.add(p);
}
}
测试
public class StuFrame {
public static ArrayList<Account> usersList;
public static Reader fw;
static File user;
static String file="stulist.txt";
public static void main(String[] args)throws Exception{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
usersList=new ArrayList<Account>();
File users=new File("stu.txt");
if(!users.exists()){
try{
users.createNewFile();
Writer fw = new FileWriter("stu.txt");
fw.write("admin 12345"+"\n");
fw.flush();
fw.close();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
usersListRead();
user=new File(file);
if(!user.exists()){
try{
user.createNewFile();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
//Login
LoginGui loginGui=new LoginGui();
}
public static void usersListRead(){
try{
fw=new FileReader("stu.txt");//字符流
}catch(Exception e){
System.out.println("字符流创建失败");
}
BufferedReader bfr=new BufferedReader(fw);
String temp;
try{
//System.out.println("开始写入list");
while((temp=bfr.readLine())!=null){
String[] tmpstr=temp.split("\\s+");//分割空格
Account a=new Account(tmpstr[0],tmpstr[1]);
usersList.add(a);
}
bfr.close();
fw.close();
}catch(Exception e){
System.out.println("读取用户文档失败");
}
}
}
public class StuFrame {
public static ArrayList usersList;
public static Reader fw;
static File user;
static String file=“stulist.txt”;
public static void main(String[] args)throws Exception{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
usersList=new ArrayList<Account>();
File users=new File("stu.txt");
if(!users.exists()){
try{
users.createNewFile();
Writer fw = new FileWriter("stu.txt");
fw.write("admin 12345"+"\n");
fw.flush();
fw.close();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
usersListRead();
user=new File(file);
if(!user.exists()){
try{
user.createNewFile();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
//Login
LoginGui loginGui=new LoginGui();
}
public static void usersListRead(){
try{
fw=new FileReader("stu.txt");//字符流
}catch(Exception e){
System.out.println("字符流创建失败");
}
BufferedReader bfr=new BufferedReader(fw);
String temp;
try{
//System.out.println("开始写入list");
while((temp=bfr.readLine())!=null){
String[] tmpstr=temp.split("\\s+");//分割空格
Account a=new Account(tmpstr[0],tmpstr[1]);
usersList.add(a);
}
bfr.close();
fw.close();
}catch(Exception e){
System.out.println("读取用户文档失败");
}
}
}