1、 课 程 设 计 报 告 学院、系: 吉林大学珠海学院计算机科学与技术系 专业名称: 网络工程 课程设计科目 java程序课程设计 学生姓名: 王佳 指导教师: 吕健波 完成时间: 2010年9月-11月 教师管理系统 一、 设计任务与目标 1)负责教师管理的主界面(用菜单方式打开下面的四个功能界面) 录入教师管理的界面 修改教师管理的界面 查询教师管理的界面 删除教师管理的界面 2)教师数据表的字段要求至少包含:工号、姓名、年龄、
2、职称、所在系、教授的主要课程、联系电话、联系地址等字段。要求字段类型设计合理、使用方便。 3)要求界面美观大方、功能实现完整 此程序的设计,主要包括两个主体部分,一是界面部分,二是创建按钮并监听,实现录入,修改,查询,删除等功能。 以前在C++编写过类似的教师管理系统,其主要思路和本题保持一致,但代码不同。 需要准备的工作是创建主界面并完善各项分功能,连接数据库,实现录入和查看同步。此次设计将会提高我对真正的JAVA程序设计的实战能力,对以前学过的知识融会贯通,并且深化和应用。意在最终程序可以美观,实用,人性化,尽最大的能力扩充相关分模块。 二、 方案设计与论证
3、针对这道设计题的特点,首先做一个界面,界面中包括相关的分功能,录入,修改,查询,删除等,用surface做界面,分别设置各自的监听器,监听每一个button,并且各个分功能是对应各自的类。这样的话,有利于用户提出不同修改的要求,此时只要单独修改个别类即可,提高程序的便捷化并且满足不同客户的需要。在做这道题时遇到最大的障碍就是连接access数据库,因为以前没学过,所以这部分知识完全不会,必须考自学。自己做这道题与其他设计人员最大的不同就在于将每个分功能,各自单独的写在一个类中,与把所有源代码都写在一个JAVA文件中的方法,更易于修改和编辑。 三、 程序框图或流程图,程序清单与调用关系
4、 教师信息管理系统主界面 Surface Ext(退出) Fin(查询) Del(删除) Chan(修改) Ads(录入) 对数据库中的内容进行编辑 Access数据库 四、全部源程序清单 package parent_window; //(package parent window包中的类) import java.awt.*; import javax.swing.*; import son_windows.*; public class surF
5、ace extends JFrame{ JButton findSt,addSt,chanSt,delSt,exitSt;//查找,添加,删除,退出 //构造函数 public surFace(){ Container c=this.getContentPane(); c.setLayout(new GridLayout(2,1)); JPanel lowerPanel=new JPanel(); c.setFont(new Font("plain",Font.PLAIN,13)); JLabel label=new JLab
6、el("欢迎进入教师信息管理系统",SwingConstants.CENTER); label.setFont(new Font("BOLD",Font.BOLD,30)); c.add(label); //创建按钮 addSt=new JButton("录入"); addSt.setToolTipText("添加教师信息"); findSt=new JButton("查询"); findSt.setToolTipText("查询信息"); chanSt=new JButton("修改"); chanSt.s
7、etToolTipText("修改成绩"); delSt=new JButton("删除"); delSt.setToolTipText("删除信息"); exitSt=new JButton("退出"); exitSt.setToolTipText("安全退出本系统"); lowerPanel.add(findSt); lowerPanel.add(addSt); lowerPanel.add(chanSt); lowerPanel.add(delSt); lowerPanel.add(exitSt
8、); c.add(lowerPanel); //注册监听器,新建独立监听器类 查找:fin(),添加:ads(),删除:del(),退出本体统:ext() findSt.addActionListener(new fin()); addSt.addActionListener(new ads()); chanSt.addActionListener(new cc()); //chanSt.addActionListener(new chan()); delSt.addActionListener(new del());
9、 exitSt.addActionListener(new ext()); } //main方法,实现主界面 public static void main (String args[]){ surFace su=new surFace(); su.setSize(500,400); su.setTitle("欢迎使用教师管理系统!"); su.setVisible(true); } } /* *建立独立事件监听器类ads,处理surFace中“录入”点击事件 */ package son
10、windows; //(package son_windows包中的类) (1) import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; public class ads extends JFrame implements ActionListener{ JTextField numberText,nameText,ageText,jobText,departmenText,classText,phoneText,addressText;
11、 JButton addButton,clearButton,closeButton; Container c2; JDialog jd; JDialog jd2; public ads(){ c2=this.getContentPane(); c2.setLayout(new GridLayout(3,1)); JPanel p1=new JPanel(new GridLayout(8,2)); JPanel p2=new JPanel(new FlowLayout()); // JLabel
12、logoLabel=new JLabel("ii",SwingConstants.CENTER); logoLabel.setForeground(Color.black); logoLabel.setFont(new Font("TRUE",Font.TRUETYPE_FONT,20)); logoLabel.setText("教师信息"); c2.add(logoLabel); //创建JTextField numberText=new JTextField(); nameText=new JTextField();
13、 ageText=new JTextField(); jobText=new JTextField(); departmenText=new JTextField(); classText=new JTextField(); phoneText=new JTextField(); addressText=new JTextField(); //添加JTextField p1.add(new JLabel("工号",SwingConstants.CENTER)); p1.add(numberText);
14、 p1.add(new JLabel("姓名",SwingConstants.CENTER)); p1.add(nameText); p1.add(new JLabel("年龄",SwingConstants.CENTER)); p1.add(ageText); p1.add(new JLabel("职称",SwingConstants.CENTER)); p1.add(jobText); p1.add(new JLabel("所在系",SwingConstants.CENTER)); p1.add(departm
15、enText); p1.add(new JLabel("主授课程",SwingConstants.CENTER)); p1.add(classText); p1.add(new JLabel("联系电话",SwingConstants.CENTER)); p1.add(phoneText); p1.add(new JLabel("联系地址",SwingConstants.CENTER)); p1.add(addressText); c2.add(p1) ; // addButton=new JButton
16、"添加"); clearButton=new JButton("清除"); closeButton=new JButton("退出"); p2.add(addButton); p2.add(clearButton); p2.add(closeButton); c2.add(p2); this.setSize(500,400); this.setTitle("添加教师信息"); //为添加按钮添加监听器(使用匿名内部类处理点击事件) addButto
17、n.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String number,name,age,job,departmen,clas,phone,address; Statement stm; String str; int j=0; number=numberText.getText(); name=nameText.getText();
18、 age=ageText.getText(); job=jobText.getText(); departmen=departmenText.getText(); clas=classText.getText(); phone=phoneText.getText(); address=addressText.getText(); str="insert into data1 values('"+number+"','"+name+"','"+age+"','"+jo
19、b+"','"+departmen+"','"+clas+"','"+phone+"','"+address+"')"; if(number.equals("")&&name.equals("")&&age.equals("")&&job.equals("")&&departmen.equals("")&&clas.equals("")&&phone.equals("")&&address.equals("")) j=2; else try { //加载驱动 Class.forName("sun.j
20、dbc.odbc.JdbcOdbcDriver"); //连接数据源forStu Connection con=DriverManager.getConnection("jdbc:odbc:forStu","",""); stm=con.createStatement(); j=stm.executeUpdate(str); con.close(); } catch (ClassNotFoundException e1) { }catch (
21、SQLException e1) { } // jd=new JDialog(); jd.setLayout(new GridLayout(2,1)); jd.setSize(255,100); JPanel p1=new JPanel(); JPanel p2=new JPanel(); jd.add(p1); jd.add(p2); JLabel jl=new JLabel("添加成功!"); JLabel jl2=new JLabel("不能添加空数据,请
22、从新输入!"); JLabel jl3=new JLabel("不能为相同数据,请从新输入!"); JButton b=new JButton("确定"); if(j==1){ p1.add(jl); j=0; }else if(j==2) p1.add(jl2); else p1.add(jl3); p2.add(b); jd.setVisible(true); b.addActionListener(new ActionListener(){
23、 public void actionPerformed(ActionEvent e){ jd.setVisible(false); } }); } }); //添加“清除”键事件监听器 clearButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ numberText.setText(""); nameText.setText("");
24、 ageText.setText(""); jobText.setText(""); departmenText.setText(""); classText.setText(""); phoneText.setText(""); addressText.setText(""); } }); //添加“关闭窗口”按钮事件监听器 closeButton.addActionListener(new ActionListener(){ public void acti
25、onPerformed(ActionEvent e){ ads.this.setVisible(false); ads.this.dispose(); } }); } //新建类ads的actionPerformed方法,实现surFace中的录入事件界面 public void actionPerformed(ActionEvent e){ ads a=new ads(); a.setVisible(true); } } (2) import java.awt.*; import jav
26、a.awt.event.*; import javax.swing.*; public class cc extends JFrame implements ActionListener{ Container c1; public cc(){ c1=this.getContentPane(); c1.setLayout(new GridLayout(4,1)); JPanel jp1=new JPanel(); JPanel jp2=new JPanel(); JPanel jp3=new JPanel(); JLabel jl=new JL
27、abel("请选择要修改那种信息",SwingConstants.CENTER); jl.setFont(new Font("BOLD",Font.BOLD,30)); c1.add(jl); JButton b1=new JButton("联系电话"); JButton b2=new JButton("联系地址"); JButton b3=new JButton(" 取消 "); jp1.add(b1); jp2.add(b2); jp3.add(b3); c1.add(jp1); c1.add(jp2); c1.
28、add(jp3); b1.addActionListener(new chan_tel()); b2.addActionListener(new chan_adr()); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dispose(); } }); } public void actionPerformed(ActionEvent e){ new cc(); setSize(
29、500,400); setVisible(true); } } (3) import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class chan_adr extends JFrame implements ActionListener{ Container c1; JPanel jp1,jp2,jp3,jp4; JLabel jl,jl2,jl3; JTextField jt1,jt2; JButto
30、n b1,b2,b3; JDialog jd; public chan_adr(){ c1=this.getContentPane(); c1.setLayout(new GridLayout(4,1)); jl=new JLabel("修改教师联系地址"); jl.setFont(new Font("BOLD",Font.BOLD,30)); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); jp1.add(jl); c1.add(
31、jp1); jl2=new JLabel("教师工号:"); jl3=new JLabel("联系地址:"); jt1=new JTextField(10); jt2=new JTextField(10); jp2.add(jl2); jp2.add(jt1); jp3.add(jl3); jp3.add(jt2); c1.add(jp2); c1.add(jp3); b1=new JButton("确定"); b2=new JButton("清除"); b3=new JButton("退出"); jp4.ad
32、d(b1); jp4.add(b2); jp4.add(b3); c1.add(jp4); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Connection con=null; Statement st; String str=null; int i=0; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); c
33、on=DriverManager.getConnection("jdbc:odbc:forStu","",""); str="update data1 set 联系地址='"+jt2.getText()+"' where 工号='"+jt1.getText()+"'"; st=con.createStatement(); i=st.executeUpdate(str); con.close(); jd=new JDialog(); jd.setVisible(true); jd.setLayout(new Gr
34、idLayout(2,1)); JPanel jpp1=new JPanel(); JPanel jpp2=new JPanel(); JLabel jll1=new JLabel("修改成功!"); JLabel jll2=new JLabel("信息不存在,请核实后重试!"); if(i==1) jpp1.add(jll1); else jpp1.add(jll2); JButton jbb=new JButton("确定"); jpp2.add(jbb);
35、 jd.add(jpp1); jd.add(jpp2); jd.setSize(255,100); jbb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jd.setVisible(false); } }); }catch(ClassNotFoundException e1){ }catch(SQLException e1){
36、 } } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jt1.setText(""); jt2.setText(""); } }); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ setVisible(false); d
37、ispose(); } }); } public void actionPerformed(ActionEvent e){ this.dispose(); chan_adr c=new chan_adr(); c.setSize(500,400); c.setVisible(true); } } (4) import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class c
38、han_tel extends JFrame implements ActionListener{ Container c1; JPanel jp1,jp2,jp3,jp4; JLabel jl,jl2,jl3; JTextField jt1,jt2; JButton b1,b2,b3; JDialog jd; public chan_tel(){ c1=this.getContentPane(); c1.setLayout(new GridLayout(4,1)); jl=new JLabel("修改教师联系电话"); jl.setFont
39、new Font("BOLD",Font.BOLD,30)); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jp4=new JPanel(); jp1.add(jl); c1.add(jp1); jl2=new JLabel("教师工号:"); jl3=new JLabel("联系电话:"); jt1=new JTextField(10); jt2=new JTextField(10); jp2.add(jl2); jp2.add(jt1); jp3.
40、add(jl3); jp3.add(jt2); c1.add(jp2); c1.add(jp3); b1=new JButton("确定"); b2=new JButton("清除"); b3=new JButton("退出"); jp4.add(b1); jp4.add(b2); jp4.add(b3); c1.add(jp4); b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Con
41、nection con=null; Statement st; String str=null; int i=0; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:forStu","",""); str="update data1 set 联系电话='"+jt2.getText()+"' where 工号='"+jt1.getText()+"'"; st=con.cr
42、eateStatement(); i=st.executeUpdate(str); con.close(); jd=new JDialog(); jd.setVisible(true); jd.setLayout(new GridLayout(2,1)); JPanel jpp1=new JPanel(); JPanel jpp2=new JPanel(); JLabel jll1=new JLabel("修改成功!"); JLabel jll2=new JLabel("信息不存在,请核
43、实后重试!"); if(i==1) jpp1.add(jll1); else jpp1.add(jll2); JButton jbb=new JButton("确定"); jpp2.add(jbb); jd.add(jpp1); jd.add(jpp2); jd.setSize(255,100); jbb.addActionListener(new ActionListener(){ public void actionPerformed(ActionE
44、vent e){ jd.setVisible(false); } }); }catch(ClassNotFoundException e1){ }catch(SQLException e1){ } } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jt1.setText(""); jt2.setText
45、""); } }); b3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ setVisible(false); dispose(); } }); } public void actionPerformed(ActionEvent e){ this.dispose(); chan_tel c=new chan_tel(); c.setSize(500,400);
46、 c.setVisible(true); } } (5) /* * 建立独立事件监听器类del,处理surFace中“删除”点击事件 */ import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class del extends JFrame implements ActionListener{ JTextField text1,text2,text3; Container c3; JDialog jd;
47、 public del(){ //删除信息GUI设计 c3=this.getContentPane(); c3.setLayout(new GridLayout(3,1)); c3.setFont(new Font("true",Font.TRUETYPE_FONT,13)); JPanel p1=new JPanel(); JPanel p4=new JPanel(); // JLabel label=new JLabel("删除教师信息",SwingConstants.CENTE
48、R); label.setFont(new Font("TRUE",Font.TRUETYPE_FONT,20)); label.setForeground(Color.black); c3.add(label); // JLabel label1=new JLabel("请输入教师工号"); text1=new JTextField(10); p1.add(label1); p1.add(text1); c3.add(p1); //
49、 JButton b1=new JButton("确定"); JButton b2=new JButton("清除"); JButton b3=new JButton("退出"); p4.add(b1); p4.add(b2); p4.add(b3); c3.add(p4); // this.setSize(500,400); this.setTitle("删除教师信息"); //内部匿名类实现“确定”按钮事件 b1.ad
50、dActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Connection con; Statement st; String str1; String s1=text1.getText(); int i=0; try{ //加载驱动,连接数据源forStu Class.forName("sun.jdbc.odbc.Jdbc






