1、用户需求分析: 英汉词典作为一个常见学习工具,是我们常常要使用。该系统能完成一个简单电子词功效。该系统关键用于实现英汉互译功效,系统拥有自己数据库。 1.英译汉功效:我们能够先选择让系统进行英译汉功效,然后在查找框中输入想要查询英文单词,系统将自动在数据库中查找匹配统计并给出相对应汉语意思。 2.汉译英功效:我们能够在系统中选择汉译英功效,然后在查找框中输入想要查询汉语意思,系统将自动在数据库中查找匹配统计并给出相对应英文单词 3.词汇添加功效:用户能够添加词库内没有单词及其解释。添加成功后该单词将在次库内保留,方便下次查询。 4.词汇修改功效;用户能够实现对词库中已经有单词及其解释
2、修改。修改后结果将保留在词库中。 5.词汇删除功效;用户可自行删除词库中已经有单词,同时次单词解释也将被一同删除。 6.其它功效:另外,系统还含有帮助和相关等功效,用来辅助用户更方便简练使用电子词典。 package dianzicidian; import java.awt.*; import .*; import java.sql.*; import java.awt.event.*; import javax.swing.JOptionPane; import java.io.*; import sun.audio.*; class dzcd exten
3、ds Frame implements ActionListener { MenuBar menubar=new MenuBar();//菜单 Menu fileMenu,editMenu,helpMenu; MenuItem fileenglish,filechinese,exit,editAdd,editmod,editDel; TextField inputtext; TextArea txt; Label label1,label2; Button btn1,btnsound; Panel p,p1,p2,p3; dzcd() {
4、super("电子词典"); setBounds(200,300,350,400); setMenuBar(menubar); fileMenu=new Menu("文件"); editMenu=new Menu("编辑"); helpMenu=new Menu("帮助"); fileenglish=new MenuItem("英汉词典"); filechinese=new MenuItem("汉英词典"); exit=new MenuItem("退出"); editAdd=new MenuItem("添加词汇");
5、editmod=new MenuItem("修改词汇"); editDel=new MenuItem("删除词汇"); menubar.add(fileMenu); menubar.add(editMenu); menubar.add(helpMenu); fileMenu.add(fileenglish); fileMenu.add(filechinese); fileMenu.addSeparator(); fileMenu.add(exit); editMenu.add(editAdd); editMenu.ad
6、d(editmod); editMenu.add(editDel); inputtext=new TextField("",10); txt=new TextArea(10,10); label1=new Label("输入要查询英语单词:"); label2=new Label("查询结果:"); btn1=new Button("查询"); btnsound=new Button("发音"); p=new Panel(new BorderLayout()); p2=new Panel(new FlowLayout(Flo
7、wLayout.LEFT,5,0)); p2.add(label1); p2.add(inputtext); p2.add(btn1); p2.add(btnsound); add(p2,"North"); p.add(label2,"North"); p.add(txt,"Center"); add(p,"Center"); setVisible(true); setResizable(false); validate(); fileenglish.addActionListener(this);
8、 filechinese.addActionListener(this); exit.addActionListener(this); editAdd.addActionListener(this); editmod.addActionListener(this); editDel.addActionListener(this); btn1.addActionListener(this); btnsound.addActionListener(this); addWindowListener(new WindowAdapter()
9、{ public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource()==fileenglish)//英汉(外观改变) { label1.setText("输入要查询英语单词:"); label2.setText("查询结果:"); txt.setText(""); btn1.setLabel("查询");
10、 btnsound.setVisible(true); } else if(e.getSource()==filechinese)//汉英(外观改变) { label1.setText("输入要查询汉语词语:"); label2.setText("查询结果:"); txt.setText(""); btn1.setLabel("查询"); btnsound.setVisible(true); } else if(e.getSource()==exit)//退出 { System.exit(0);
11、} else if(e.getSource()==btn1) { if(btn1.getLabel().equals("查询"))//实现查询功效(包含英汉或汉英) { txt.setText(null); try { Listwords(); } catch(SQLException ee){} } else if(btn1.getLabel().equals("提交"))//实现添加功效 { try { addwords(); }
12、 catch(SQLException ee){} } else if(btn1.getLabel().equals("更新"))//实现修改功效 { try { modwords(); } catch(SQLException ee){} } else if(btn1.getLabel().equals("删除"))//实现删除功效 { try { delwords(); } catch(SQLException ee){} }
13、 } else if(e.getSource()==editAdd)//添加(外观改变) { label1.setText("输入新单词:"); label2.setText("输入汉字解释:"); btn1.setLabel("提交"); btnsound.setVisible(false); } else if(e.getSource()==editmod)//修改(外观改变) { label1.setText("输入要修改单词:"); label2.setText("输入更新后解释:"); btn1
14、setLabel("更新"); btnsound.setVisible(false); } else if(e.getSource()==editDel)//删除(外观改变) { label1.setText("输入要删除单词:"); label2.setText(""); btn1.setLabel("删除"); btnsound.setVisible(false); } else if(e.getSource()==btnsound)//发音 { if(inputtext.getText()!=null)
15、 { try { InputStream is=getClass().getResource("sound//"+ inputtext.getText().trim()+".wav").openStream(); AudioPlayer.player.start(is); } catch(IOException e1){} } } }
16、 public void Listwords() throws SQLException//查询实现过程 { String cname,ename; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:words","",""); Statement Ex1Stmt=Ex1Con.createS
17、tatement(); ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo=false; while((boo=rs.next())==true) { ename=rs.getString("英语"); cname=rs.getString("汉语"); if(ename.equals(inputtext.getText())&& label1.getText().equals("输入要查询英语单词:")) { txt.append(
18、cname); break; } else if(cname.equals(inputtext.getText())&& label1.getText().equals("输入要查询汉语词语:")) { txt.append(ename); break; } } Ex1Con.close(); if(boo==false) { JOptionPane.showMessageDialog(this,"查无此单词!","警告", JOptionPane.WARNING_MESSAGE);
19、 } } public void addwords() throws SQLException//向数据库添加新词汇 { String cname,ename; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:words","",""); Statement Ex1Stmt=
20、Ex1Con.createStatement(); ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo=false; while((boo=rs.next())==true) { ename=rs.getString("英语"); cname=rs.getString("汉语"); if(ename.equals(inputtext.getText())&&cname.equals(txt.getText())) { JOptionPane.
21、showMessageDialog(this,"此词汇已存在!","警告", JOptionPane.WARNING_MESSAGE); break; } } if(boo==false) { Ex1Stmt.executeUpdate("INSERT INTO words (英语,汉语) VALUES ('"+ inputtext.getText().trim()+"','"+txt.getText().trim()+"')"); JOptionPane.showMessageDialog(this,"添加成功!","恭喜
22、", JOptionPane.WARNING_MESSAGE); } Ex1Con.close(); } public void modwords() throws SQLException//修改词库中统计 { String ename; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} Connection Ex1Con=DriverManager.getConnecti
23、on("jdbc:odbc:words","",""); Statement Ex1Stmt=Ex1Con.createStatement(); ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo=false; while((boo=rs.next())==true) { ename=rs.getString("英语"); if(ename.equals(inputtext.getText())) { Ex1Stmt.executeUpdate
24、"UPDATE words SET 汉语='"+txt.getText().trim() +"' WHERE 英语='"+inputtext.getText().trim()+"'"); JOptionPane.showMessageDialog(this,"统计修改成功!","恭喜", JOptionPane.WARNING_MESSAGE); break; } } Ex1Con.close(); if(boo==false) { JOptionPane.showMessageDialog(this,"不存在
25、此单词!","警告", JOptionPane.WARNING_MESSAGE); } } public void delwords() throws SQLException//删除词库中统计 { @SuppressWarnings("unused") String cname,ename; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} Connection Ex1Con=
26、DriverManager.getConnection("jdbc:odbc:wordskechengsheji","",""); Statement Ex1Stmt=Ex1Con.createStatement(); ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM words"); boolean boo=false; while((boo=rs.next())==true) { ename=rs.getString("英语"); cname=rs.getString("汉语"); if(
27、ename.equals(inputtext.getText())) { Ex1Stmt.executeUpdate("DELETE FROM words WHERE 英语='"+ inputtext.getText().trim()+"'"); JOptionPane.showMessageDialog(this,"成功删除统计!","恭喜", JOptionPane.WARNING_MESSAGE); break; } } Ex1Con.close(); if(boo==false) { JOptionPane.showMessageDialog(this,"不存在此单词!","警告", JOptionPane.WARNING_MESSAGE); } } public static void main(String args[]) { new dzcd(); } }






