比上一个基于Mysql数据库的完善了一些细节
WindowIO是窗口界面
package JavaStuIO; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; //class student class student implements java.io.Serializable{ String number; String name; String sex; String birthday; String department; public student(String number,String name,String sex,String birthday,String department){ this.number = number; this.name = name; this.sex = sex; this.birthday = birthday; this.department = department; } } public class WindowIO extends JFrame { //private static final long serialVersionUID = -1928970409928880648L; public static void main(String[] args){ ///////////////////////////////////////////////////////////////////////////// TreeMap<String,student> tm = new TreeMap<String,student>(); //treemap // File file =new File("E:\\java用文件","file.txt"); if(!file.exists()) { FileOutputStream fout = null; ObjectOutputStream oos = null; try{ fout = new FileOutputStream(file); oos = new ObjectOutputStream(fout); oos.writeObject(tm); fout.close(); oos.close(); System.out.println("write ok!!!"); }catch (Exception er){ er.printStackTrace(); System.out.println("writeobject go die"); } } ///////////////////////////////////////////////////////////////////////// JFrame jframe = new JFrame("学生管理系统") ; JLabel title = new JLabel("这是一个学生管理系统",JLabel.CENTER); Font font = new Font("",Font.BOLD+Font.PLAIN,15); title.setFont(font); ImageIcon icon = new ImageIcon("E:\\java用图片\\look.jpg"); icon.setImage(icon.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT)); JLabel pic = new JLabel(icon); //pic.setSize(10, 10); //pic.setSize(50,100);????? //pic.drawImage(???,100, 100, 50, 50,null); jframe.setSize(450,300); jframe.setLocation(250,350); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); JButton button1 = new JButton("添加"); JButton button2 = new JButton("修改"); JButton button3 = new JButton("查询"); JButton button4 = new JButton("删除"); JButton button5 = new JButton("浏览"); FlowLayout flow = new FlowLayout(FlowLayout.LEFT,10,10); JPanel panel = new JPanel(flow); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(button5); panel.add(title); panel.add(pic); jframe.add(panel); try{ file.createNewFile(); System.out.println("creat ok!"); } catch (IOException e){ System.out.println("creat go die!"); e.printStackTrace(); } button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Add add = new Add(); } }); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Change change = new Change(); } }); button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Ask ask = new Ask(); } }); button4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Delete delete = new Delete(); } }); button5.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Look look = new Look(); } }); } }
Add是添加功能
package JavaStuIO; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.util.TreeMap; import java.io.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import JavaStuIO.WindowIO; public class Add extends JFrame{ JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JTextField jtname = new JTextField("",20); JTextField jtsex = new JTextField("",20); JTextField jtbirthday = new JTextField("",20); JTextField jtdepartment = new JTextField("",20); JButton buttonadd = new JButton("添加"); JButton buttonreturn = new JButton("返回"); public Add() { //TreeMap<String,student> tm = new TreeMap<String,student>(); JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonadd); jpforbutton.add(buttonreturn); buttonadd.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Add // File file =new File("E:\\java用文件","file.txt"); // // String name = jtname.getText() + " "; // String number = jtnumber.getText() + " "; // String sex = jtsex.getText() + " "; // String birthday = jtbirthday.getText() + " "; // String department = jtdepartment.getText()+"\r\n"; // // student stu = new student(number,name,sex,birthday,department); // // Writer out = null; // try{ // out = new FileWriter(file,true); // out.write(stu.number); // out.write(stu.name); // out.write(stu.sex); // out.write(stu.birthday); // out.write(stu.department); // // // out.close(); // System.out.println("write ok!"); // }catch (IOException ee){ // System.out.println("write go die!"); // ee.printStackTrace(); // // } File file =new File("E:\\java用文件","file.txt"); FileInputStream fin = null; ObjectInputStream ois = null; try{ fin = new FileInputStream(file); // ois = new ObjectInputStream(fin); // TreeMap<String,student> tm = (TreeMap)ois.readObject(); //??? fin.close(); ois.close(); //insert String name = jtname.getText(); String number = jtnumber.getText(); String sex = jtsex.getText(); String birthday = jtbirthday.getText(); String department = jtdepartment.getText(); student stu = new student(number,name,sex,birthday,department); FileOutputStream fout = null; ObjectOutputStream oos = null; try{ fout = new FileOutputStream(file); // oos = new ObjectOutputStream(fout); // tm.put(stu.number,stu); oos.writeObject(tm); fout.close(); oos.close(); System.out.println("write ok!!!"); JOptionPane.showMessageDialog(null,"世界线已经发生了变动 el psy congroo!"); }catch (Exception er){ er.printStackTrace(); System.out.println("writeobject go die"); } // }catch(Exception et){ System.out.println("go die!"); et.printStackTrace(); } jtnumber.setText(null); jtname.setText(null); jtsex.setText(null); jtbirthday.setText(null); jtdepartment.setText(null); //TreeMap<String,student> tm = new TreeMap<String,student>(); // String name = jtname.getText(); // String number = jtnumber.getText(); // String sex = jtsex.getText(); // String birthday = jtbirthday.getText(); // String department = jtdepartment.getText(); // // student stu = new student(number,name,sex,birthday,department); // // // // FileOutputStream fout = null; // ObjectOutputStream oos = null; // try{ // fout = new FileOutputStream(file); // // oos = new ObjectOutputStream(fout); // // // tm.put(stu.number,stu); // // oos.writeObject(tm); // // fout.close(); // oos.close(); // // System.out.println("write ok!!!"); // // // }catch (Exception er){ // er.printStackTrace(); // System.out.println("writeobject go die"); // } // // jtnumber.setText(null); // jtname.setText(null); // jtsex.setText(null); // jtbirthday.setText(null); // jtdepartment.setText(null); //add end // // }} ); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ } }); this.setTitle("添加学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Ask是查询
package JavaStuIO; import java.sql.*; import java.util.TreeMap; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import JavaStuIO.WindowIO; public class Ask extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JLabel jname = new JLabel(); JLabel jsex = new JLabel(); JLabel jbirthday = new JLabel(); JLabel jdepartment = new JLabel(); JButton buttonask = new JButton("查询"); JButton buttonreturn = new JButton("返回"); public Ask() { JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jname); jpsex.add(jlsex); jpsex.add(jsex); jpbirthday.add(jlbirthday); jpbirthday.add(jbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jdepartment); jpforbutton.add(buttonask); jpforbutton.add(buttonreturn); buttonask.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //ask String number = jtnumber.getText(); File file =new File("E:\\java用文件","file.txt"); //TreeMap<String,student> tm = new TreeMap<String,student>(); FileInputStream fin = null; ObjectInputStream ois = null; try{ fin = new FileInputStream(file); // ois = new ObjectInputStream(fin); // TreeMap<String,student> tm = (TreeMap)ois.readObject(); //??? student stu = (student)tm.get(number); if(tm.containsKey(number)) { jname.setText(stu.name); jsex.setText(stu.sex); jbirthday.setText(stu.birthday); jdepartment.setText(stu.department); } else { System.out.println("number is not exist!"); JOptionPane.showMessageDialog(null,"学号:excuse me?", "warning",JOptionPane.WARNING_MESSAGE); } fin.close(); ois.close(); }catch(Exception et){ System.out.println("go die!"); et.printStackTrace(); } //end ask }} ); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Window window = new Window(); } }); this.setTitle("查询学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Change是修改
package JavaStuIO; import java.sql.*; import java.util.TreeMap; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import JavaStuIO.WindowIO; public class Change extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JTextField jtname = new JTextField("",20); JTextField jtsex = new JTextField("",20); JTextField jtbirthday = new JTextField("",20); JTextField jtdepartment = new JTextField("",20); JButton buttonchange = new JButton("修改"); JButton buttonreturn = new JButton("返回"); public Change() { JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonchange); jpforbutton.add(buttonreturn); buttonchange.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String number = jtnumber.getText(); String name = jtname.getText(); String sex = jtsex.getText(); String birthday = jtbirthday.getText(); String department = jtdepartment.getText(); //change File file = new File ("E:\\java用文件","file.txt"); FileInputStream fin = null; ObjectInputStream ois = null; try{ fin = new FileInputStream(file); // ois = new ObjectInputStream(fin); // TreeMap<String,student> tm = (TreeMap)ois.readObject(); //??? fin.close(); ois.close(); //insert if(tm.containsKey(number)){ student stu = new student(number,name,sex,birthday,department); tm.put(stu.number,stu); FileOutputStream fout = null; ObjectOutputStream oos = null; try{ fout = new FileOutputStream(file); oos = new ObjectOutputStream(fout); oos.writeObject(tm); oos.close(); JOptionPane.showMessageDialog(null,"世界线已经发生了变动 el psy congroo!"); }catch (IOException er){ er.printStackTrace(); System.out.println("writeobject go die"); } } else{ System.out.println("number is not exist!"); JOptionPane.showMessageDialog(null,"学号:excuse me?", "warning",JOptionPane.WARNING_MESSAGE); } // }catch(Exception et){ System.out.println("go die!"); et.printStackTrace(); } //TreeMap<String,student> tm = new TreeMap<String,student>(); jtnumber.setText(null); jtname.setText(null); jtsex.setText(null); jtbirthday.setText(null); jtdepartment.setText(null); //change end } }); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Window window = new Window(); } }); this.setTitle("修改学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Delete是删除功能
package JavaStuIO; import java.sql.*; import java.util.TreeMap; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.html.HTMLDocument.Iterator; import java.io.*; import JavaStuIO.WindowIO; import JavaStuIO.Add; public class Delete extends JFrame { private static final long serialVersionUID = -1928970409928880648L; //TreeMap<String,student> tm = new TreeMap<String,student>(); //treemap JLabel jlnumber = new JLabel("学号:"); JTextField jtnumber = new JTextField("",20); JButton buttondelete = new JButton("删除"); JButton buttonreturn = new JButton("返回"); public Delete() { JPanel jpnumber = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpforbutton.add(buttondelete); jpforbutton.add(buttonreturn); buttondelete.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //delete String number = jtnumber.getText(); File file =new File("E:\\java用文件","file.txt"); //TreeMap<String,student> tm = new TreeMap<String,student>(); FileInputStream fin = null; ObjectInputStream ois = null; try{ fin = new FileInputStream(file); // ois = new ObjectInputStream(fin); // TreeMap<String,student> tm = (TreeMap)ois.readObject(); //??? //insert if(tm.containsKey(number)) { tm.remove(number); //remove System.out.println("delete ok!!!"); jtnumber.setText(null); FileOutputStream fout = null; ObjectOutputStream oos = null; try{ fout = new FileOutputStream(file); // oos = new ObjectOutputStream(fout); // oos.writeObject(tm); // oos.close(); JOptionPane.showMessageDialog(null,"这个人已经不存在了呢 "); }catch (IOException er){ er.printStackTrace(); System.out.println("writeobject go die"); } } else { System.out.println("number is not exist!"); JOptionPane.showMessageDialog(null,"学号:excuse me?", "warning",JOptionPane.WARNING_MESSAGE); } //insert end fin.close(); ois.close(); }catch(Exception et){ System.out.println("go die!"); et.printStackTrace(); } // if(tm.isEmpty()) // { // System.out.println("!!!!!!!!!!!!"); // // } //delete end E:\\java用文件","file.txt } }); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Window window = new Window(); } }); this.setTitle("删除学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Look是浏览功能
package JavaStuIO; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; import JavaStuIO.WindowIO; public class Look extends JFrame { private static final long serialVersionUID = -1928970409928880648L; Connection conn = null; PreparedStatement ps = null; ResultSet res = null; //JButton buttonlook = new JButton("浏览"); //JButton buttonreturn = new JButton("返回"); JTable jtable; JScrollPane jscrollpane = new JScrollPane(); Vector columnNames = null; Vector rowData = null; public Look() { JPanel jpforbutton = new JPanel(new GridLayout(1,1)); columnNames = new Vector(); columnNames.add("学号"); columnNames.add("姓名"); columnNames.add("性别"); columnNames.add("出生日期"); columnNames.add("学院"); rowData = new Vector(); JTextArea jtext = new JTextArea(16,30); //jpforbutton.add(buttonlook); //jpforbutton.add(buttonreturn); File file =new File("E:\\java用文件","file.txt"); FileInputStream fin = null; ObjectInputStream ois = null; try{ fin = new FileInputStream(file); // ois = new ObjectInputStream(fin); // TreeMap<String,student> tm = (TreeMap)ois.readObject(); //??? //insert if(tm.isEmpty()) { System.out.println("nobody here!"); JOptionPane.showMessageDialog(null,"还没有人惹"); } else { Set<Map.Entry<String, student>> tmSet = tm.entrySet(); Iterator<Map.Entry<String,student>> treeIt = tmSet.iterator(); while(treeIt.hasNext()) { Map.Entry<String , student> treeEntry = treeIt.next(); Vector hang = new Vector(); student stu = treeEntry.getValue(); hang.add(stu.number); hang.add(stu.name); hang.add(stu.sex); hang.add(stu.birthday); hang.add(stu.department); rowData.add(hang); } } //insert end fin.close(); ois.close(); }catch(Exception et){ System.out.println("go die!"); et.printStackTrace(); } jtable = new JTable(rowData,columnNames); jscrollpane = new JScrollPane(jtable); this.add(jscrollpane); this.setTitle("浏览学生信息"); this.setLayout(new GridLayout(2,5)); this.add(jpforbutton); this.setLocation(300,300); this.setSize(500,300); this.setVisible(true); this.setResizable(false); } }
今天的文章java简单学生管理系统_编写学生成绩管理系统分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/45363.html