您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页数据结构课程设计报告(简易英汉词典系统)

数据结构课程设计报告(简易英汉词典系统)

来源:华佗小知识


数据结构课程设计报告(简易英汉词典系统)

广西大学

课程设计报告

课程: 《数据结构》 题目:简易英汉词典系统

学 院:______计算机与电子信息学院_ 专 业:______计网类___________ 班 级:______计网071_____ ___ _ 学 号: 0707100103 学生姓名: 蓝冠恒 指导教师:__ ________

二〇〇九 年 四 月

一、实验目的和要求

【实验目的】设计一个简易英汉词典系统,实现基本的单词检索功能,即输入英文单词则系统输出其汉语,反之亦然;并能够进行基本的词典维护操作,包括插入和删除,要求按英语的词典顺序排列单词。 【设计要求】

1

(1)使用图形化界面。

(2)建立英语词汇表,输入为小写字母时为合法输入。

(3)每个词条应包含单词的英语形式、汉语释义、发音等基本信息。

二、设计概要:

1、根据实验要求,与及所了解的数据类型相关知识,定义了如下数据类型:

链接数据库声明的变量:

Connection connection = null; Statement statement = null; ResultSet Result = null; 声明全局变量:

String newWord, pronunciations, indexWord=\"\searchEnglish, searchChinese,isSound;

它们分别是:新增单词、新单词音标、引擎词、删除词、查找的英文、查找的汉语、判断音频是否存在。

2、实验功能所定义的各种按钮、文本框、选择框和列表框:

取消删除功能按钮:

JButton deleteCancelButton = new JButton(); 英汉互译面板上的发音按钮:

JButton sound = new JButton(); 浏览词库面板上的发音按钮:

JButton scanSoundButton = new JButton(); 添加单词的按钮:

JButton addOkJButton = new JButton(); 取消添加功能的按钮:

JButton addCancelButton = new JButton(); 删除词库单词信息按钮:

JButton deleteOkButton = new JButton(); 浏览词库按钮:

JButton scanButton = new JButton(); 要查找的英文或汉语的文本框输入框:

JTextField inputSearchWord = new JTextField();

1

新增单词文本输入框:

JTextField inputNewWord = new JTextField(); 新增单词音标输入框:

JTextField pronunciation = new JTextField(); 新增单词释义输入框:

JTextField newWordChinese = new JTextField(); 新增单词过去式输入框:

JTextField newWordPreterite = new JTextField(); 新增单词现在进行时态输入框:

JTextField newWordModernism = new JTextField(); 要删除的单词输入框:

JTextField inputDeleteWord = new JTextField(); 新增单词词性复选框:

JComboBox newWordClass = new JComboBox(); 列出要查找的词的记录信息的多行文本框:

TextArea outputSearchEnglish = new TextArea(); 列出要删除的单词的记录信息的多行文本框:

TextArea deleteWordChinese = new TextArea(); 列出浏览时需浏览单词信息的多行文本框: TextArea scanOutput = new TextArea();

DefaultListModel model = new DefaultListModel(); 列出所查找的英文或汉语对应的单词的列表框:

JList listSearchEnglish = new JList(model); 列出备选英标的列表框:

JList pronunciationJList = new JList(listItem); 列出可能且可以被删除的单词的列表框: JList deleteJList = new JList(model); 列出词库所有单词的列表框:

JList scanWordJList = new JList(model);

3、实现功能所定义的各种函数:

1)public static String Return(String str)

将数据库里的单词的音标进行还原。因为有些音标不能正确存入数据库,只能以其它字符代替,读取时再还原。 2)public void Play(String word)

1

播放单词word的音频。

3)public void inputSearch_caretUpdate(CaretEvent e) 当inputSearch(注:要查找的词)输入框值改变时,listSearchEnglish(注:列出查找结果)列表框和outputSearchEnglish(注:写出单词信息)文本输出框被清空,取inputSearch输入框值赋给indexWord,如果indexWord不为空,连接数据库;再判断要查找的是英语,还是汉语,根据判断结果进行查找,并将结果集按升序排列,输出到listSearchEnglish列表框

4)public void listSearchEnglish_mouseClicked(MouseEvent me) 当鼠标单击listSearchEnglish列表框时,取所选选项的值赋给变量searchEnglish,去掉其两端空格,outputSearchEnglish文本输出框被清空,发音按钮变成灰白色,从数据库读取要查找的单词,将其音标进行还原,将单词信息输出到outputSearchEnglish文本框,如果可以发音,发音按钮变红色,否则indexWord被赋给空值 5)public void sound_actionPerformed(ActionEvent e)

如果查找单词有音频且查找词不为空,则可发音,否则按钮变为灰白色

6)public void inputNewWord_caretUpdate(CaretEvent e) 一旦输入的新单词字母不是小写字母或空格时,弹出提示框

7)public void pronunciation_caretUpdate(CaretEvent e)

对输入的音标进行处理,因为字符\ɔ、\ǝ、‘、\æ 存入数据库时会变成?或出错,所以用0、1、2、4分别代替他们,读取时通过调用Return函数还原,且将音标两端和中间空格去掉

8)public void pronunciationJList_mouseClicked(MouseEvent me)

当在音标选项列表单击鼠标选择音标时,将所选音标输入音标输入框

9)public void addOkJButton_actionPerformed(ActionEvent e) 读取未读取的输入框的字符,去掉单词、注释各自两端空格,并将去掉它们各自中间两个或两个以上的空格留一个空格,接着在数据库查找添加的单词是否已存在,若已存在,弹出提示框,不能再添加,否则向数据库添加新单词信息,最后清空所有输入框

10)public void addCancelButton_actionPerformed(ActionEvent

e)

点击添加取消按钮,新增面板所有输入框被清空

11)public void inputDeleteWord_caretUpdate(CaretEvent e)

1

当inputDeleteWord输入框值改变时,清空deleteJList列表框,读取inputDeleteWord输入框值赋给变量deleteWord,按模糊查找法查找以deleteWord的值开头的所有单词,按升序排列,并将他们输出到deleteJList列表框

12)public void deleteJList_mouseClicked(MouseEvent me) 当在deleteJList列表框单击选项时,读取选项值赋给deleteWord变量,并去掉其两端空格,向inputDeleteWord输入框输入deleteWord变量值,接着从数据库读取要删除的单词的信息,如某属性不为空,将其输入到eleteWordChinese多行文本框 13)public void deleteOkButton_actionPerformed(ActionEvent e) 单击删除按钮,如果输入框为空,则弹出提示框,否则从数据库中删除要删除的单词的所有记录

14)Public void

deleteCancelButton_actionPerformed(ActionEvent e)

单击取消按钮,inputDeleteWord、deleteWordChinese都被清空 15)public void scanButton_actionPerformed(ActionEvent e)

单击浏览按钮,scanWordJList列表框被清空,然后从数据库读取所有单词,按升序排列;然后将所得结果集输入到scanWordJList列表框

16)public void scanWordJList_mouseClicked(MouseEvent me)

当鼠标单击scanWordJList列表框时,取所选选项的值赋给变量ndexWord,去掉其两端空格,scanOutput文本框被清空,发音按钮变成灰白色,从数据库读取要查找的单词(indexWord的值),将其音标进行还原,将单词信息输出到scanOutput文本框,如果可以发音,发音按钮变红色,否则indexWord被赋给空值

17)public void soundButton_actionPerformed(ActionEvent e)

如果查找单词有音频且查找词不为空,则可发音,否则按钮变为灰白色

三、程序模块流程图

1

开始 单击发音按是否Y 可否Y 列出单Y 单列出找Y 是否弹出提是否添加选择 单击添保存所Y 提示单击取成Y 写入音弹出警输入Y 是否N 不能显示结清空所Y N 结N 英汉互译 清空列Y 是否N 查找查找显错N 是否Y 输入N 1

输列出找删除单击Y Y 模糊Y 是否N N 结列出单词弹出提N 点击删是否Y 删除点击取清空各单击浏列出单浏览可否Y 发音按发音Y 按升序列出是否N 结Y N 单击

1

四、具体程序设计代码

package languanhengdictionary;

import java.awt.BorderLayout; import java.sql.*; import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.DataLine; import javax.swing.JFrame;

import javax.swing.JTabbedPane; import java.awt.Rectangle; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Font; import java.awt.Color;

import javax.swing.JTextField; import javax.swing.JButton;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextArea; import java.awt.TextArea; import java.awt.Dimension; import java.awt.Choice;

import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.Checkbox;

import javax.swing.JComboBox; import javax.swing.JList;

import javax.swing.JOptionPane; import java.awt.*;

import javax.swing.JScrollPane;

import java.awt.event.MouseAdapter;

1

import java.awt.event.MouseEvent; import javax.swing.DefaultListModel;

import java.awt.event.HierarchyListener; import java.awt.event.HierarchyEvent; import javax.swing.event.CaretListener; import javax.swing.event.CaretEvent; import javax.swing.border.TitledBorder; import javax.swing.UIManager;

import javax.swing.BorderFactory; import java.io.*;

import javax.swing.border.Border;

public class englishFrame extends JFrame { Connection connection = null; Statement statement = null; ResultSet Result = null; public englishFrame() { try {

jbInit();

} catch (Exception exception) { exception.printStackTrace(); } }

private void jbInit() throws Exception { getContentPane().setLayout(null);

mainJP.setFont(new java.awt.Font(\"宋体\25));

mainJP.setForeground(new Color(0, 242, 118)); mainJP.setBorder(titledBorder1); mainJP.setToolTipText(\"\");

mainJP.setBounds(new Rectangle(2, 51, 543, 295)); searchEnglishOrChineseJP.setLayout(null); addWordJP.setLayout(null); deleteJP.setLayout(null); scanStock.setLayout(null);

jLabel2.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

1

jLabel2.setForeground(new Color(0, 180, 62)); jLabel2.setText(\"输入引擎词\");

jLabel2.setBounds(new Rectangle(8, 16, 106, 43)); inputSearchWord.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

jScrollPane1.setBounds(new Rectangle(118, 60, 145, 178));

listSearchEnglish.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

listSearchEnglish.setForeground(Color.blue); listSearchEnglish.setSelectionBackground(new Color(51, 255, 104));

outputSearchEnglish.setFont(new java.awt.Font(\"宋体\

outputSearchEnglish.setBounds(new Rectangle(267, 59, 227, 179));

jLabel4.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel4.setForeground(new Color(0, 180, 0)); jLabel4.setText(\"新单词\");

jLabel4.setBounds(new Rectangle(6, 9, 81, 34)); inputNewWord.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

inputNewWord.setBounds(new Rectangle(93, 11, 155, 34));

jLabel5.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel5.setForeground(new Color(0, 159, 14)); jLabel5.setText(\"发 音\");

jLabel5.setBounds(new Rectangle(6, 118, 76, 33)); jLabel6.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel6.setForeground(new Color(0, 173, 55)); jLabel6.setText(\"词 性\");

jLabel6.setBounds(new Rectangle(7, 63, 81, 35)); pronunciation.setFont(new java.awt.Font(\"Arial Unicode MS\

pronunciation.setBounds(new Rectangle(92, 119, 154,

1

36));

newWordClass.setBounds(new Rectangle(95, 62, 154, 36));

jScrollPane3.setBounds(new Rectangle(93, 156, 154, 79));

jLabel7.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel7.setForeground(new Color(0, 208, 48)); jLabel7.setText(\"注 释\");

jLabel7.setBounds(new Rectangle(267, 9, 72, 34)); newWordChinese.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

newWordChinese.setBounds(new Rectangle(354, 11, 1, 34));

jLabel8.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel8.setForeground(new Color(0, 187, 0)); jLabel8.setToolTipText(\"\"); jLabel8.setText(\"过去式\");

jLabel8.setBounds(new Rectangle(2, 62, 77, 32)); newWordPreterite.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

newWordPreterite.setBounds(new Rectangle(353, 61, 1, 36));

jLabel9.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel9.setForeground(new Color(0, 187, 21)); jLabel9.setText(\"现代式\");

jLabel9.setBounds(new Rectangle(263, 122, 75, 31)); newWordModernism.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

newWordModernism.setBounds(new Rectangle(353, 121, 163, 37));

addOkJButton.setBounds(new Rectangle(261, 179, 83, 40));

addOkJButton.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

addOkJButton.setText(\"确定\");

1

addCancelButton.setBounds(new Rectangle(407, 182, 83, 37));

addCancelButton.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

addCancelButton.setText(\"取消\");

jLabel10.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 25));

jLabel10.setForeground(new Color(0, 187, 62)); jLabel10.setText(\"输入词汇\");

jLabel10.setBounds(new Rectangle(6, 33, 107, 34)); inputDeleteWord.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

inputDeleteWord.setBounds(new Rectangle(112, 31, 186, 37));

deleteWordChinese.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

deleteWordChinese.setBounds(new Rectangle(112, 69, 185, 168));

jScrollPane4.setBounds(new Rectangle(297, 31, 139, 206));

deleteJList.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

deleteJList.setForeground(Color.blue);

deleteJList.setSelectionBackground(new Color(51, 255, 0));

deleteOkButton.setBounds(new Rectangle(437, 31, 83, 38));

deleteOkButton.setFont(new java.awt.Font(\"楷体\Font.PLAIN, 20));

deleteOkButton.setForeground(new Color(0, 187, 55)); deleteOkButton.setText(\"删除\");

scanButton.setForeground(new Color(0, 214, 0)); scanButton.setText(\"浏览\");

jScrollPane5.setBounds(new Rectangle(25, 59, 146, 178));

scanOutput.setColumns(5);

scanOutput.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

1

scanOutput.setBounds(new Rectangle(188, 60, 315, 177));

scanWordJList.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

scanWordJList.setForeground(Color.blue);

scanWordJList.setSelectionBackground(Color.green); jLabel11.setFont(new java.awt.Font(\"楷体\Font.PLAIN, 45));

jLabel11.setForeground(new Color(0, 255, 138)); jLabel11.setText(\"欢迎访问词库\");

jLabel11.setBounds(new Rectangle(197, 8, 287, 40)); deleteCancelButton.setBounds(new Rectangle(437, 96, 83, 34));

deleteCancelButton.setFont(new java.awt.Font(\"楷体\

deleteCancelButton.setForeground(new Color(0, 221, 42));

deleteCancelButton.setToolTipText(\"\"); deleteCancelButton.setText(\"取消\");

pronunciationJList.setSelectionBackground(new Color(51, 255, 125));

sound.setBounds(new Rectangle(420, 24, 74, 33)); sound.setFont(new java.awt.Font(\"宋体\20));

sound.setToolTipText(\"\"); sound.setText(\"发音\");

inputSearchWord.setBounds(new Rectangle(117, 24, 302, 35));

scanButton.setBackground(new Color(240, 240, 240)); scanButton.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

scanSoundButton.setBounds(new Rectangle(110, 15, 79, 37));

scanSoundButton.setFont(new java.awt.Font(\"宋体\Font.PLAIN, 20));

scanSoundButton.setText(\"发音\");

scanButton.setBounds(new Rectangle(27, 14, 78, 38)); this.getContentPane().add(mainJP);

1

searchEnglishOrChineseJP.add(jLabel2);

searchEnglishOrChineseJP.add(jScrollPane1);

searchEnglishOrChineseJP.add(outputSearchEnglish); searchEnglishOrChineseJP.add(inputSearchWord); searchEnglishOrChineseJP.add(sound);

jScrollPane1.getViewport().add(listSearchEnglish); mainJP.add(searchEnglishOrChineseJP, \"英汉互译\"); mainJP.add(addWordJP, \"添加词汇\"); addWordJP.add(jLabel4);

addWordJP.add(inputNewWord); addWordJP.add(jLabel7);

addWordJP.add(newWordChinese); addWordJP.add(newWordClass); addWordJP.add(jLabel6);

addWordJP.add(newWordPreterite); addWordJP.add(jLabel5); addWordJP.add(jLabel9);

addWordJP.add(newWordModernism); addWordJP.add(jScrollPane3); addWordJP.add(pronunciation); addWordJP.add(addOkJButton); addWordJP.add(addCancelButton); addWordJP.add(jLabel8);

jScrollPane3.getViewport().add(pronunciationJList); mainJP.add(deleteJP, \"删除词汇\"); deleteJP.add(jLabel10);

deleteJP.add(inputDeleteWord); deleteJP.add(deleteWordChinese); deleteJP.add(jScrollPane4); deleteJP.add(deleteOkButton);

deleteJP.add(deleteCancelButton);

jScrollPane4.getViewport().add(deleteJList); mainJP.add(scanStock, \"浏览词库\"); scanStock.add(jScrollPane5);

jScrollPane5.getViewport().add(scanWordJList); scanStock.add(scanButton); scanStock.add(scanOutput);

1

scanStock.add(jLabel11);

scanStock.add(scanSoundButton);

this.getContentPane().add(jLabel1);

jLabel1.setFont(new java.awt.Font(\"楷体\Font.PLAIN, 45));

jLabel1.setForeground(Color.blue); jLabel1.setText(\"蓝氏简易英汉词典\");

jLabel1.setBounds(new Rectangle(107, 5, 374, 47)); newWordClass.addItem(\"n. 名词\");

newWordClass.addItem(\"adj. 形容词\"); newWordClass.addItem(\"adv. 副词\"); newWordClass.addItem(\"num. 量词\"); newWordClass.addItem(\"V. 动词\");

newWordClass.addItem(\"Vi. 不及物动词\"); newWordClass.addItem(\"Vt. 及物动词\"); newWordClass.addItem(\"短语\");

pronunciationJList.setLayoutOrientation(pronunciationJList.HORIZONTAL_WRAP);

this.setSize(new Dimension(560, 385)); //第一个参

//-----------------英汉互译模块函数调用------------------// //发音按钮

sound.addActionListener(new ActionListener() {

1

/

/为长度,第二个参数为高度

public void actionPerformed(ActionEvent e) { sound_actionPerformed(e); } });

//查找输入框

inputSearchWord.addCaretListener(new CaretListener()

{

public void caretUpdate(CaretEvent e) { inputSearch_caretUpdate(e); } });

//查找单词列表

listSearchEnglish.addMouseListener(new

MouseAdapter() {

public void mouseClicked(MouseEvent e) { listSearchEnglish_mouseClicked(e); } });

//--------------------添加面板模块函数调----------------// //新单词输入框

inputNewWord.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { inputNewWord_caretUpdate(e); } });

//新单词音标输入框

pronunciation.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { pronunciation_caretUpdate(e); } });

//音标选择列表鼠标

pronunciationJList.addMouseListener(new

MouseAdapter() {

public void mouseClicked(MouseEvent e) { pronunciationJList_mouseClicked(e); } });

1

//添加按钮

addOkJButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addOkJButton_actionPerformed(e); } });

// 取消添加单词按钮

addCancelButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e) { addCancelButton_actionPerformed(e); } });

//-------------------删除模块函数调用-------------------//

//删除单词输入框

inputDeleteWord.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { inputDeleteWord_caretUpdate(e); } });

//删除单词列表框鼠标

deleteJList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { deleteJList_mouseClicked(e); } });

//删除按钮

deleteOkButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deleteOkButton_actionPerformed(e); } });

//取消删除按钮

deleteCancelButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { deleteCancelButton_actionPerformed(e);

1

} });

//-----------------浏览数据库函数调用-------------------// //浏览词库单词发音按钮

scanSoundButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { soundButton_actionPerformed(e); } });

//浏览词库按钮

scanButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { scanButton_actionPerformed(e); } });

//浏览单词列表鼠标

scanWordJList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { scanWordJList_mouseClicked(e); } }); }

String listItem[] = {\"/\\"'\\\"b\\"d\\"e\

\"h\\"g\\"i:\\"i\\"j\\"k\\"r\\"s\η\θ\з\\"ε\

\"∧\ɑ\ɑ:\\"au\∫\∫\

};

DefaultListModel model = new DefaultListModel(); JTabbedPane mainJP = new JTabbedPane(); JLabel jLabel1 = new JLabel();

JPanel searchEnglishOrChineseJP = new JPanel(); JPanel addWordJP = new JPanel(); JPanel deleteJP = new JPanel(); JPanel scanStock = new JPanel(); JLabel jLabel2 = new JLabel();

JTextField inputSearchWord = new JTextField();

1

JScrollPane jScrollPane1 = new JScrollPane(); JList listSearchEnglish = new JList(model); TextArea outputSearchEnglish = new TextArea(); JLabel jLabel4 = new JLabel();

JTextField inputNewWord = new JTextField(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel();

JTextField pronunciation = new JTextField(); JComboBox newWordClass = new JComboBox();

JScrollPane jScrollPane3 = new JScrollPane(); JList pronunciationJList = new JList(listItem); JLabel jLabel7 = new JLabel();

JTextField newWordChinese = new JTextField(); JLabel jLabel8 = new JLabel();

JTextField newWordPreterite = new JTextField(); JLabel jLabel9 = new JLabel();

JTextField newWordModernism = new JTextField(); JButton addOkJButton = new JButton(); JButton addCancelButton = new JButton(); JLabel jLabel10 = new JLabel();

JTextField inputDeleteWord = new JTextField(); TextArea deleteWordChinese = new TextArea(); JScrollPane jScrollPane4 = new JScrollPane(); JList deleteJList = new JList(model); JButton deleteOkButton = new JButton(); JButton scanButton = new JButton();

JScrollPane jScrollPane5 = new JScrollPane(); JList scanWordJList = new JList(model); TextArea scanOutput = new TextArea(); JLabel jLabel11 = new JLabel();

String newWord, indexWord=\"\deleteWord, searchEnglish, searchChinese,isSound;

String pronunciations; //发音变量

JButton deleteCancelButton = new JButton();

Border border1 = BorderFactory.createEmptyBorder(); TitledBorder titledBorder1 = new TitledBorder(\"\"); JButton sound = new JButton();

JButton scanSoundButton = new JButton();

1

/*方法简介:由于特殊音标字符无法存入数据库,只能以其他字

符代替存入数据库,从数据库读取输出时再还原,这里用0、1、2、4分别代替字符ɔ、ə 、‘、æ 通过str.replace()替换还原*/

public static String Return(String str) { int i,length;

length=str.length();

for( i=1;i<=length;i++){

str =str.replace(\"1\ str=str.replace(\"0\ str=str.replace(\"2\

str=str.replace(\"4\ length--; }

return str; }

/*方法简介:根据要查找的单词(去掉两端空格),在音频文件找匹配的音频,通过输入流进行转换,将其播放(限于wav格式)*/ public void Play(String word){

SourceDataLine sdl = null; word=word.trim(); String fileurl = \"Database/sound/\"+word+\".wav\"; try{

AudioInputStream ais =

AudioSystem.getAudioInputStream(new File(fileurl)); AudioFormat aif = ais.getFormat(); DataLine.Info info = new DataLine.Info(SourceDataLine.class,aif); sdl =

(SourceDataLine)AudioSystem.getLine(info); sdl.open(aif); sdl.start(); //play

int nByte = 0;

byte[] buffer = new byte[128]; while(nByte != -1){

nByte = ais.read(buffer,0,128);

1

if(nByte >= 0){

int oByte = sdl.write(buffer, 0, nByte);

} }

sdl.stop();

}catch(UnsupportedAudioFileException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace();

} catch (LineUnavailableException e) { e.printStackTrace(); } }

//--------------------英汉互译模块方法-------------------//

/*方法简介:当inputSearch输入框值改变时,

listSearchEnglish列表框和outputSearchEnglish文本输出框被清空,取inputSearch输入框值赋给indexWord,如果indexWord不为空,连接数据库;再判断要查找的是英语,还是汉语,根据判断结果进行查找,并将结果集按升序排列,输出到listSearchEnglish列表框*/

public void inputSearch_caretUpdate(CaretEvent e) {

((DefaultListModel)listSearchEnglish.getModel()).removeAllElements();

outputSearchEnglish.setText(\"\");

indexWord = inputSearchWord.getText(); indexWord= indexWord.trim(); if(!indexWord.equals(\"\")) {

char[] Is=indexWord.toCharArray(); try { try {

Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\"); } catch (ClassNotFoundException ex1) {} connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft

1

Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\ statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"温馨提示\ }

if(Is[0]>96 && Is[0]<123 ){ searchEnglish=indexWord;

String sql= \"select * from wordstock where word LIKE '\"+searchEnglish+\"%'order by word ASC\";

try { Result = statement.executeQuery(sql); while (Result.next()) {

model.addElement(Result.getString(\"word\")+\" \\n\");

listSearchEnglish.setModel(model); }

}catch(SQLException

ex ){ JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"温馨提示\ } } else{

searchChinese=indexWord;

String sql= \"select * from wordstock where chinese LIKE '%\"+searchChinese+\"%'order by word ASC\";

try { Result = statement.executeQuery(sql); while (Result.next()) {

model.addElement(Result.getString(\"word\")+\" \\n\");

listSearchEnglish.setModel(model); }

connection.close(); statement.close(); }catch(SQLException

ex ){ JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"温馨提示\ }

1

} } }

/*方法简介:当鼠标单击listSearchEnglish列表框时,取所选

选项的值赋给变量searchEnglish,去掉其两端空格 outputSearchEnglish文本输出框被清空,发音按钮变成灰,白色,从数据库读取要查找的单词,将其音标进行还原,将单词信息输出到outputSearchEnglish文本框,如果可以音,发音按钮变红色,否则indexWord被赋给空值*/

public void listSearchEnglish_mouseClicked(MouseEvent me) {

if(me.getClickCount() == 1){ //单击 searchEnglish = (String) listSearchEnglish.getSelectedValue();

searchEnglish = searchEnglish.trim(); if (!searchEnglish.equals(\"\")) { try {

try {

Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) {} connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\ statement = connection.createStatement();

} catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

indexWord = searchEnglish;

outputSearchEnglish.setText(\"\");

sound.setBackground(new Color(240, 240, 240));

String sql = \"select * from wordstock where word='\" +

1

searchEnglish + \"'\"; try {

Result = statement.executeQuery(sql); if (Result.next()) { String Wordclass = Result.getString(\"wordClass\");

String Chinese = Result.getString(\"chinese\");

String Pronunciation = Return(Result.getString(\"pronunciation\")); String Preterite = Result.getString(\"preterite\");

String Modernism = Result.getString(\"modernism\");

outputSearchEnglish.append(\"单词: \" +Result.getString(\"word\") + \"\\n\");

if (!Pronunciation.equals(\"\")) outputSearchEnglish.append(\"发音: \" + Pronunciation +\"\\n\");

if (!Wordclass.equals(\"\"))

outputSearchEnglish.append(\"词性: \" + Wordclass +\"\\n\");

outputSearchEnglish.append(\"注释: \" + Chinese + \"\\n\");

if (!Preterite.equals(\"\"))

outputSearchEnglish.append(\"过去式: \" + Preterite +\"\\n\"); if

(!Modernism.equalsIgnoreCase(\"\"))

outputSearchEnglish.append(\"现在式: \" + Modernism +\"\\n\"); }

isSound = Result.getString(\"sound\"); if (isSound.equals(\"Y\")) {

sound.setBackground(new Color(255, 0, 79));

} else

1

{ indexWord=\"\";} connection.close(); statement.close();

} catch (SQLException ex2) {

scanOutput.append(ex2.getMessage()); } } } }

//方法简介:如果查找单词有音频且查找词不为空,则可发音,

//否则按钮变为灰白色

public void sound_actionPerformed(ActionEvent e) { if(isSound.equals(\"Y\")&&!indexWord.equals(\"\") ){ Play(indexWord); }

else{ sound.setBackground(new Color(240, 240, 240)); } }

//----------------------添加模块方法--------------------// //方法简介:当输入的新单词不是小写字母或空格时,弹出提示框 public void inputNewWord_caretUpdate(CaretEvent e) { newWord = inputNewWord.getText(); char[] Is=newWord.toCharArray(); int length=newWord.length(); for( int i=0;iif(!((Is[i]>96 && Is[i]<123 )|| Is[i]==32)){

JOptionPane.showMessageDialog(englishFrame.this,\"只能输入小写字母和空格\\n否则会出错!\温馨提示\ inputNewWord.setText(\"\"); break; } } }

/*方法简介:对输入的音标进行处理,因为字符ɔ、ә、‘、 æ 存入数据库时会变成?或出错,所以用0、1、2、4分别代替他们 读取时通过调用Return函数还原,且将音标两端和中间空格去掉*/

1

public void pronunciation_caretUpdate(CaretEvent e) { int i,length;

pronunciations= pronunciation.getText(); length=pronunciations.length();

for( i=1;i<=length;i++){ //去掉音标中的两个或两个以上的空格留一个空格且进行替换特殊字符

pronunciations =pronunciations.replace(\" \ pronunciations =pronunciations.replace(\"\ə\\"1\");

pronunciations =pronunciations.replace(\"\ɔ\\"0\");

pronunciations =pronunciations.replace(\"'\ pronunciations =pronunciations.replace(\"\æ\\"4\");

length--; }

pronunciations =pronunciations.replace(\" \ pronunciations =pronunciations.trim(); }

/*方法简介:当在音标选项列表单击鼠标选择音标时,将所选音标输入音标输入框*/

public void pronunciationJList_mouseClicked(MouseEvent me) {

if(me.getClickCount() == 1){ //单击

String

e=(String)pronunciationJList.getSelectedValue(); pronunciations= pronunciation.getText()+e; pronunciation.setText(pronunciations); } }

/*方法简介:读取未读取的数据,去掉单词、注释各自两端空格,

并将去掉它们各自中间两个或两个以上的空格留一个空格,接着在数据库查找添加的单词是否已存在,如已存在,弹出提示框,不能再添加,否则向数据库添加新单词信息,最后清空所有输入框*/

public void addOkJButton_actionPerformed(ActionEvent e) {

1

int length,i;

String chinese = newWordChinese.getText(); String wordClass = (String) newWordClass.getSelectedItem();

String wordPeterite=newWordPreterite.getText(); String wordModernism= newWordModernism.getText(); String search;

length=newWord.length();

for( i=1;i<=length;i++){ //去掉单词中的两个或两个以上的空格留一个空格

newWord=newWord.replace(\" \ length--; }

newWord=newWord.trim();//去掉单词两端空格 length=chinese.length();

for( i=1;i<=length;i++){ //去掉汉语释义中的两个或两个以上的空格留一个空格

chinese=chinese.replace(\" \ length--; }

chinese=chinese.trim();//去掉汉译两端空格 JOptionPane msg = new JOptionPane();

try { try {

Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\"); } catch (ClassNotFoundException ex1) {} connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\

statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

String sql = \"INSERT INTO wordstock

1

values('\"+newWord+\"','\"+pronunciations+\"','\"+wordClass+\"','\"+chinese+\"','\"+wordPeterite+\"','\"+wordModernism+\"','N')\";

search = \"select * from wordstock where word='\"+newWord+\"'\";

if(newWord.equals(\"\"))

{JOptionPane.showMessageDialog(englishFrame.this, \"请输入单词!\温馨提示\ else{ try {

Result = statement.executeQuery(search); if (Result.next()) {

JOptionPane.showMessageDialog(englishFrame.this, \"词库已存在该单词\\n不能再添加 \"+Result.getString(\"word\"),\"温馨提示\1);

connection.close(); statement.close(); }

else {

statement.executeUpdate(sql);

JOptionPane.showMessageDialog(englishFrame.this, \"单词加入成功\添加单词\

connection.close(); statement.close(); }

} catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this,ex.getMessage(), \"单词添加数据库失败\ }

pronunciation.setText(\"\"); inputNewWord.setText(\"\"); newWordChinese.setText(\"\"); newWordPreterite.setText(\"\"); newWordModernism.setText(\"\"); }

1

}

//方法简介:点击取消按钮,所有输入框被清空

public void addCancelButton_actionPerformed(ActionEvent e) {

newWord=null;

inputNewWord.setText(newWord); newWordChinese.setText(\"\"); pronunciation.setText(\"\"); newWordPreterite.setText(\"\"); newWordModernism.setText(\"\"); }

//------------------------删除模块方法-------------------//

/*方法简介:当inputDeleteWord输入框值改变时,清空

deleteJList列表框,读取inputDeleteWord输入框值赋给变量deleteWord,按模糊查找法查找以deleteWord的值开头的所有单词,按升序排列,并将他们输出到deleteJList列表框*/ public void inputDeleteWord_caretUpdate(CaretEvent e) { ((DefaultListModel)deleteJList.getModel()).removeAllElements();

deleteWord=inputDeleteWord.getText(); try { try {

Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) {} connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\

statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

String sql= \"select * from wordstock where word LIKE '\"+deleteWord+\"%'order by word ASC\";

1

try { Result = statement.executeQuery(sql); while (Result.next()) {

model.addElement(Result.getString(\"word\")+\" \\n\"); deleteJList.setModel(model); }

connection.close(); statement.close(); }catch(SQLException

ex ){ JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

/*方法简介:当在deleteJList列表框单击选项时,读取选项值赋给deleteWord变量,并去掉其两端空格,向inputDeleteWord输入框输入deleteWord变量值,接着从数据库读取要删除的单词的信息,如某属性不为空,将其输入到eleteWordChinese多行文本框*/ public void deleteJList_mouseClicked(MouseEvent me) { if(me.getClickCount() == 1){ deleteWord = (String) deleteJList.getSelectedValue();

deleteWord = deleteWord.trim();

inputDeleteWord.setText(deleteWord); try { try

{ Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) { } connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access

Driver(*.mdb)};DBQ=Database\\\\englishWord.mdb\

statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

deleteWordChinese.setText(\"\");

String sql=\"select * from wordstock where

1

word='\"+deleteWord+\"'\"; try {

Result = statement.executeQuery(sql);

if( Result.next()){ String

Wordclass=Result.getString(\"wordClass\"); String

Chinese=Result.getString(\"chinese\"); String

Pronunciation=Return(Result.getString(\"pronunciation\")); String Preterite= Result.getString(\"preterite\");

String Modernism= Result.getString(\"modernism\");

deleteWordChinese.append(\"单词: \"+ Result.getString(\"word\")+\"\\n\");

if(!Pronunciation.equals(\"\")) deleteWordChinese.append(\"发音: \"+ Pronunciation+\"\\n\");

if(!Wordclass.equals(\"\"))

deleteWordChinese.append(\"词性: \"+Wordclass+\"\\n\");

deleteWordChinese.append(\"注释: \"+Chinese +\"\\n\");

if(!Preterite.equals(\"\"))

deleteWordChinese.append(\"过去式: \"+Preterite+\"\\n\");

if(! Modernism.equals(\"\"))

deleteWordChinese.append(\"现在式: \"+Modernism+\"\\n\");

}

connection.close(); statement.close(); }catch (SQLException ex2) { scanOutput.append(ex2.getMessage()); } } }

1

/*方法简介:单击删除按钮,如果输入框为空,则弹出提示框,

否则从数据库中删除要删除的单词的所有记录*/

public void deleteOkButton_actionPerformed(ActionEvent e) {

try {

try

{ Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) { } connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\

statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"温馨提示\ }

String sql=\"delete *from wordstock where word='\"+deleteWord+\"'\";

if(deleteWord.equals(\"\"))

{ JOptionPane.showMessageDialog(englishFrame.this,\"请输入你要删除的单词!\ else

{try {deleteWordChinese.setText(\"\"); statement.executeQuery(sql); connection.close(); statement.close();

} catch (SQLException ex2) {

JOptionPane.showMessageDialog(englishFrame.this,\"恭喜你,删除成功!\" ,\"\ } } }

//方法简介:单击取消按钮,inputDeleteWord、deleteWordChinese都被清空 public void

1

deleteCancelButton_actionPerformed(ActionEvent e) { inputDeleteWord.setText(\"\"); deleteWordChinese.setText(\"\"); }

//-----------------------浏览数据库模块方法------------// /*方法简介:单击浏览按钮,scanWordJList列表框被清空,后

从数据库读取所有单词,按升序排列;然后将所得结果集输入到scanWordJList列表框*/

public void scanButton_actionPerformed(ActionEvent e) {

((DefaultListModel)scanWordJList.getModel()).removeAllElements();

try { try

{ Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) { } connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\

statement = connection.createStatement(); } catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

try { Result = statement.executeQuery(\"select * from wordstock order by word ASC\");

while (Result.next()) {

model.addElement(Result.getString(\"word\")+\" \\n\");

scanWordJList.setModel(model); }

connection.close(); statement.close();

} catch (SQLException ex2) { }

}

/*方法简介:当鼠标单击scanWordJList列表框时,取所选选项的

1

值赋给变量ndexWord,去掉其两端空格,scanOutput文本框被清空,发音按钮变成灰白色,从数据库读取要查找的单词(indexWord的值),将其音标进行还原,将单词信息输出到scanOutput文本框,如果可以发音,发音按钮变红色,否则indexWord被赋给空值*/

public void scanWordJList_mouseClicked(MouseEvent me) { if(me.getClickCount() == 1){ //单击 indexWord=(String) scanWordJList.getSelectedValue();

indexWord= indexWord.trim(); scanOutput.setText(\"\");

scanSoundButton.setBackground(new Color(240, 240,240));

if(!indexWord.equals(\"\")){ try { try

{ Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");

} catch (ClassNotFoundException ex1) { }

connection =

DriverManager.getConnection(\"jdbc:odbc:Driver={MicroSoft Access Driver

(*.mdb)};DBQ=Database\\\\englishWord.mdb\ statement = connection.createStatement();

} catch (SQLException ex) {

JOptionPane.showMessageDialog(englishFrame.this, ex.getMessage(), \"连接数据库失败\ }

String sql=\"select * from wordstock where word='\"+indexWord+\"'\"; try {

Result = statement.executeQuery(sql);

if( Result.next()){ String

Wordclass=Result.getString(\"wordClass\"); String

1

Chinese=Result.getString(\"chinese\"); String

Pronunciation=Return(Result.getString(\"pronunciation\")); String Preterite= Result.getString(\"preterite\");

String Modernism= Result.getString(\"modernism\");

scanOutput.append(\"单词: \"+ Result.getString(\"word\")+\"\\n\");

if(!Pronunciation.equals(\"\")) scanOutput.append(\"发音: \"+ Pronunciation+\"\\n\");

if(!Wordclass.equals(\"\")) scanOutput.append(\"词性: \"+Wordclass+\"\\n\");

scanOutput.append(\"注释: \"+Chinese +\"\\n\");

if(!Preterite.equals(\"\")) { scanOutput.append(\"过去式: \"+Preterite+\"\\n\");}

if(! Modernism.equals(\"\")) {scanOutput.append(\"现在式: \"+Modernism+\"\\n\");}

isSound=Result.getString(\"sound\");

if(isSound.equals(\"Y\")){

scanSoundButton.setBackground(new Color(255, 0, 79)); } else

{indexWord=\"\";} }

connection.close(); statement.close();

}catch (SQLException ex2) { scanOutput.append(ex2.getMessage()); } } }

1

}

/*方法简介:如果查找单词有音频且查找词不为空,则可发音,

否则按钮变为灰白色*/

public void soundButton_actionPerformed(ActionEvent e) { if(isSound.equals(\"Y\")&&!indexWord.equals(\"\")){ Play(indexWord); }

else{ scanSoundButton.setBackground(new Color(240, 240, 240)); } }

//------------------------主函数-----------------------//

public static void main(String[] args) {

englishFrame EnglishFrame = new englishFrame(); EnglishFrame.setVisible(true);

//第四个参数为高度 第三个为长度 第一个参数为x轴 EnglishFrame.setMaximizedBounds(new Rectangle(350,

180, 560, 390));

EnglishFrame.setLocation(350, 240); //第一个参数为X,

//第二个参数为Y

} }

五、实验结果:

1

1

1

1

1

1

1

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务