Java實作帶發音的簡易電子詞典
(以下所有内容均屬作者原創,使用請注明出處!)
最近我們在做Java的綜合實訓,我的題目比較簡單,是做一個帶有發音功能的簡易電子詞典。
要求:設計一個發音電子詞典,具有對英語單詞的查詢、添加、修改、删除和讀音功能。
視窗由菜單欄、工具條和顯示欄組成。菜單欄包括檔案、編輯、幫助等菜單;工具條包括輸入欄和查詢、添加、删除、發音等按鈕;顯示欄顯示查詢單詞在詞典中的所有解釋。像類似的用Java做一個電子詞典的程式很多,但我沒有找到有帶發音功能的例子。我這道題基本上也就是實作增删改查了,再加上一個音頻檔案的播放,應該是很好實作的,我做的時候難的主要是這些音頻檔案怎麼找,而且還得是單個單詞或者詞組的音頻。最後我在一位高人那裡學到了一種方法,推薦給大家。
使用下面三個連結
http://dict.youdao.com/dictvoice?type=1&audio=forklift (英音)
http://dict.youdao.com/dictvoice?type=2&audio=forklift (美音)
http://dict.youdao.com/dictvoice?audio=road%2Broller (詞組%2B是連結符号)
把audio=後面的單詞換成需要的單詞就可以下載下傳每個單詞發音了,下載下傳下來之後改一下名稱,預設類型.mp3,用格式工廠轉換成Java能用的類型就行了。
在我的實習中,我定義一個類來存儲每個單詞的資訊。
Word.java
package dictionary;
public class Word {
String name;
String explain;
// String ridio;
Word next;
public Word(Word nextval){
next = nextval;
}
public Word(String n,String e,Word nextval){
name = n;
explain = e;
// ridio = r;
next = nextval;
}
public Word getNext(){
return next;
}
public void setNext(Word nextval){
next = nextval;
}
public String getName(){
return name;
}
public String getExplain(){
return explain;
}
// public String getRidio(){
// return ridio;
// }
public void setName(String n){
name = n;
}
public void setExplain(String e){
name = e;
}
public void setRidio(String r){
name = r;
}
}
然後定義一個連結清單類。通過方法load()讀檔案dictionary.txt(用.txt檔案便于觀察檔案内容變化)把裡面的單詞資訊存到若幹個Word對象裡面,,再把這些對象存到連結清單類中,然後定義增删改查等函數。當然,存儲資料也可以使用資料庫等其他方法,另外連結清單類也可以使用Linklist泛型類(而且這樣應該簡單些)。
Diction.java`
package dictionary;
import java.io.*;
public class Diction {
Word head;
Diction(){
head = new Word(null);
}
public void add(Word word){
word.next = head.next;
head.next = word;
}
public String search(String n){
Word p = head;
int i = 0;
while(p.next != null){
p = p.next;
if(p.name.equals(n)){
return p.explain;
}
}
if(i == 0){
return "查找失敗!";
}
return n;
}
public void change(String n,String e){
Word p = head;
Word q = p.next;
int i = 0;
while(p.next != null){
if(q.name.equals(n)){
q.explain = e;
i = 1;
break;
}
p = q;
q = p.next;
}
if(i == 0){
System.out.println("查詢單詞失敗,無法修改!");
}
}
public boolean delete(String n){
Word p = head;
Word q = p.next;
int i = 0;
while(p.next != null){
if(q.name.equals(n)){
p.next = q.next;
i = 1;
return true;
}
p = q;
q = p.next;
}
if(i == 0){
System.out.println("沒找到單詞,删除失敗!");
}
return false;
}
public void load(){
File fRead = new File(".\\dictionary.txt");
Reader in;
try {
in = new FileReader(fRead);
BufferedReader bufferRead = new BufferedReader(in);
String str = null;
while((str = bufferRead.readLine()) != null){
Word p = new Word(null);
p.name = str;
if((str = bufferRead.readLine()) != null){
p.explain = str;
p.next = head.next;
head.next = p;
}
// if((str = bufferRead.readLine()) != null){
// p.ridio = str;
// p.next = head.next;
// head.next = p;
// }
}
bufferRead.close();
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(){
File fWrite = new File(".\\dictionary.txt");
try {
Writer out = new FileWriter(fWrite);
BufferedWriter bufferWrite = new BufferedWriter(out);
Word p = head;
Word q = p.next;
while(p.next != null){
bufferWrite.write(q.name);
bufferWrite.newLine();
bufferWrite.write(q.explain);
bufferWrite.newLine();
// bufferWrite.write(q.ridio);
// bufferWrite.newLine();
p = q;
q = p.next;
}
bufferWrite.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
定義好資料結構之後,就開始寫界面了,界面設計比較簡單就不多說。增删改查也不用說,已經在Diction.java中實作了這些功能,隻需要放到界面中來。這裡我主要說實作發音功能的監聽事件,讀取文本框的單詞,在通過單詞生成音頻所在目錄,接下來就是簡單的音頻播放事件了。最後在結束的時候觸發一個視窗事件,将操作後的單詞資訊儲存至檔案中。此外,我趁還有時間加了一些亂七八糟的功能。
Window.java
public class Window extends JFrame{
Box baseBox,Box1,Box2,Box3;
JMenuBar menubar;
JMenu menu1,menu2,menu3;
JMenuItem file,edit,usehelp,about,fileHelp;
JTextField input;
JTextArea show;
JButton add;
JButton delete;
JButton change;
JButton search;
JButton speak;
Diction dic = new Diction();
searchListen listen1 = new searchListen();
addListen listen2 = new addListen();
deleteListen listen3 = new deleteListen();
changeListen listen4 = new changeListen();
saveListen listen5 = new saveListen();
helpListen listen6 = new helpListen();
speakListen listen7 = new speakListen();
editListen listen8 = new editListen();
fileListen listen9 = new fileListen();
aboutListen listen10 = new aboutListen();
fileHelpListen listen11 = new fileHelpListen();
public Window(String s,int x,int y,int w,int h){
init(s);
setLocation(x, y);
setSize(w, h);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(String s){
dic.load();
setTitle(s);
setLayout(new FlowLayout());
Box1 = Box.createHorizontalBox();
Box2 = Box.createHorizontalBox();
Box3 = Box.createHorizontalBox();
baseBox = Box.createVerticalBox();
menubar = new JMenuBar();
menu1 = new JMenu("檔案");
menu2 = new JMenu("編輯");
menu3 = new JMenu("幫助");
usehelp = new JMenuItem("使用幫助");
file = new JMenuItem("添加音頻");
edit = new JMenuItem("修改檔案");
about = new JMenuItem("關于産品");
fileHelp = new JMenuItem("添加幫助");
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menu1.add(file);
menu1.add(fileHelp);
menu2.add(edit);
menu3.add(usehelp);
menu3.add(about);
setJMenuBar(menubar);
input = new JTextField(20);
add = new JButton("增添");
delete = new JButton("删除");
change = new JButton("修改");
search = new JButton("查找");
speak = new JButton("發音");
Box1.add(new JLabel("請輸入要操作的單詞:"));
Box1.add(input);
Box2.add(search);
Box2.add(add);
Box2.add(change);
Box2.add(delete);
Box2.add(speak);
Box3.add(new JLabel("操作結果:"));
show = new JTextArea(12,12);
show.setWrapStyleWord(true);
show.setEditable(true);
show.setLineWrap(true);
show.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(show);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Box3.add(jsp);
baseBox.add(Box1);
baseBox.add(Box.createVerticalStrut(8));
baseBox.add(Box2);
baseBox.add(Box.createVerticalStrut(8));
baseBox.add(Box3);
add(baseBox);
listen1.getText(input);
listen1.getArea(show);
search.addActionListener(listen1);
listen2.getText(input);
listen2.getArea(show);
add.addActionListener(listen2);
listen3.getText(input);
delete.addActionListener(listen3);
listen4.getText(input);
listen4.getArea(show);
change.addActionListener(listen4);
addWindowListener(listen5);
usehelp.addActionListener(listen6);
edit.addActionListener(listen8);
file.addActionListener(listen9);
about.addActionListener(listen10);
fileHelp.addActionListener(listen11);
listen7.getText(input);
speak.addActionListener(listen7);
}
public interface Listen1 extends ActionListener{
public void getText(JTextField text);
}
public interface Listen2 extends ActionListener{
public void getArea(JTextArea area);
}
class searchListen implements Listen1,Listen2{
JTextField textInput;
JTextArea textShow;
public void getText(JTextField text){
textInput = text;
}
public void getArea(JTextArea area){
textShow = area;
}
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
textShow.setText(null);
if(dic.search(str).equals("查找失敗!")){
JOptionPane.showMessageDialog(null, "查找失敗!","消息對話框",JOptionPane.WARNING_MESSAGE);
}
else{
textShow.append(dic.search(str));
}
}
}
class addListen implements Listen2,Listen1{
JTextField textInput;
JTextArea textShow;
public void getText(JTextField text){
textInput = text;
}
public void getArea(JTextArea area){
textShow = area;
}
public void actionPerformed(ActionEvent e) {
String str1 = textInput.getText();
String str2 = textShow.getText();
Word word = new Word(null);
word.name = str1;
word.explain = str2;
dic.add(word);
JOptionPane.showMessageDialog(null, "添加成功!","消息對話框",JOptionPane.WARNING_MESSAGE);
}
}
class deleteListen implements Listen1{
JTextField textInput;
JTextArea textShow;
public void getText(JTextField text){
textInput = text;
}
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
if(dic.delete(str)){
JOptionPane.showMessageDialog(null, "删除成功!","消息對話框",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(null, "删除失敗!","消息對話框",JOptionPane.WARNING_MESSAGE);
}
}
class changeListen implements Listen1,Listen2{
JTextField textInput;
JTextArea textShow;
public void getText(JTextField text){
textInput = text;
}
public void getArea(JTextArea area){
textShow = area;
}
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
String str2 = textShow.getText();
dic.change(str, str2);
JOptionPane.showMessageDialog(null, "修改成功!","消息對話框",JOptionPane.WARNING_MESSAGE);
}
}
class saveListen extends WindowAdapter{
public void windowClosing(WindowEvent e){
dic.save();
System.exit(0);
}
}
class speakListen implements Listen1{
JTextField textInput;
public void getText(JTextField text) {
textInput = text;
}
public void actionPerformed(ActionEvent e) {
try{
String str = "word/"+textInput.getText()+".wav";
File file = new File(str);
URI uri = file.toURI();
URL url = uri.toURL();
AudioClip clip;
clip = Applet.newAudioClip(url);
clip.play();
}
catch(Exception e1){
System.out.print(e1.getMessage());
}
}
}
class helpListen implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "查詢:輸入單詞,點選查詢,在文本框顯示結果;\n" +
"增添:輸入單詞,在文本框輸入解釋,點選增添即可;\n" +
"删除:輸入單詞,點選删除即可将此單詞及其注釋删除;\n" +
"修改:輸入單詞,在文本框輸入修改後的注釋,點選修改;\n" +
"如需添加音頻檔案,請點選菜單項的檔案,建議添加前首先檢視添加幫助;\n" +
"另:請謹慎修改存放單詞的檔案,否則有可能造成産品崩潰。若一定要修改,請按标準格式修改!","幫助",JOptionPane.WARNING_MESSAGE);
}
}
class editListen implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
Runtime.getRuntime().exec("cmd /c start explorer .\\dictionary.txt");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class fileListen implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("cmd /c start explorer .\\word");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class aboutListen implements ActionListener{
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "産品名稱:簡易電子詞典\n作者:\n日期:2017年6月28日\n聲明:版權所有,侵權必究!使用請注明作者!","關于簡易電子詞典",JOptionPane.WARNING_MESSAGE);
}
}
class fileHelpListen implements ActionListener{
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "在菜單欄點選添加音頻選項,将需要添加的音頻檔案存放至打開的目錄下!\n\n注意:檔案命名格式必須為“單詞名.wav”,其他格式名無效\n\n音頻材料尋找方法:\n\t" +
"http://dict.youdao.com/dictvoice?type=1&audio=forklift (英音)\n\t" +
"http://dict.youdao.com/dictvoice?type=2&audio=forklift (美音)\n\t" +
"http://dict.youdao.com/dictvoice?audio=road%2Broller (詞組%2B是連結符号)\n" +
"把上面三個連結audio=後面的單詞換成任意單詞就可以下載下傳每個單詞發音了","關于簡易電子詞典",JOptionPane.WARNING_MESSAGE);
}
}
}
最後就是寫一個類實作功能了。實作之後界面就是下面這樣了,Java實作帶發音的簡易電子詞典【源碼】。