package 作业5; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.io.File; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.util.HashMap; import java.util.Scanner; public class Example13_7{ public static void main( String args []){ WindowWord win = new WindowWord(); win.setTitle("英﹣汉小字典"); } } class WindowWord extends JFrame{ JTextField inputText,showText; WordPolice police; public WindowWord(){ setLayout(new FlowLayout()); inputText = new JTextField(6); showText = new JTextField(6); showText.setColumns(15); add(inputText); add(showText); police = new WordPolice(); police.setView(this); inputText.addActionListener(police); setBounds(100,100,400,280); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class WordPolice implements ActionListener { WindowWord view; HashMap<String,String>hashtable; File file = new File ("E:\\JAVA\\上机\\作业5\\cet4.txt"); Scanner sc = null ; WordPolice () { hashtable = new HashMap< String,String >(); //System.out.println(hashtable.size()); try { sc = new Scanner ( file ); //System.out.println(sc); while (sc.hasNext ()) { String englishWord = sc . next (); String chineseWord = sc . next (); hashtable . put ( englishWord , chineseWord ); } HashMap<String,String> reversedHashtable = new HashMap<String,String>(); for (String key : hashtable.keySet()) { String value = hashtable.get(key); reversedHashtable.put(value, key); } hashtable.putAll(reversedHashtable); } catch(Exception e ) {} } public void setView(WindowWord view) { this.view = view; } public void actionPerformed(ActionEvent e) { String inputWord = view.inputText.getText (); if(hashtable.containsKey(inputWord)) { String outputWord = hashtable.get ( inputWord ); view.showText.setText (outputWord ); } else { String chineseWord = inputWord; for (String key : hashtable.keySet()) { String value = hashtable.get(key); if (value.equals(chineseWord)) { view.showText.setText(key); return; } } view.showText.setText ("没有此单词"); } } }把这段代码完善一下,让它能实现汉语翻译为英语,英语也能翻译为汉语,GUI界面不变
时间: 2023-07-16 15:15:07 浏览: 93
完善后的代码如下:
```
package 作业5;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.File;
import java.util.HashMap;
import java.util.Scanner;
public class Example13_7{
public static void main( String args []){
WindowWord win = new WindowWord();
win.setTitle("英﹣汉小字典");
}
}
class WindowWord extends JFrame{
JTextField inputText,showText;
WordPolice police;
public WindowWord(){
setLayout(new FlowLayout());
inputText = new JTextField(6);
showText = new JTextField(6);
showText.setColumns(15);
add(inputText);
add(showText);
police = new WordPolice();
police.setView(this);
inputText.addActionListener(police);
setBounds(100,100,400,280);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class WordPolice implements ActionListener {
WindowWord view;
HashMap<String,String> hashtable;
File file = new File("E:\\JAVA\\上机\\作业5\\cet4.txt");
Scanner sc = null ;
WordPolice () {
hashtable = new HashMap<String,String>();
try {
sc = new Scanner(file);
while (sc.hasNext()) {
String englishWord = sc.next();
String chineseWord = sc.next();
hashtable.put(englishWord, chineseWord);
}
HashMap<String,String> reversedHashtable = new HashMap<String,String>();
for (String key : hashtable.keySet()) {
String value = hashtable.get(key);
reversedHashtable.put(value, key);
}
hashtable.putAll(reversedHashtable);
} catch(Exception e ) {}
}
public void setView(WindowWord view) {
this.view = view;
}
public void actionPerformed(ActionEvent e) {
String inputWord = view.inputText.getText();
if(hashtable.containsKey(inputWord)) {
String outputWord = hashtable.get(inputWord);
view.showText.setText(outputWord);
} else {
String chineseWord = inputWord;
for (String key : hashtable.keySet()) {
String value = hashtable.get(key);
if (value.equals(chineseWord)) {
view.showText.setText(key);
return;
}
}
for (String value : hashtable.values()) {
if (value.equals(inputWord)) {
String outputWord = hashtable.get(value);
view.showText.setText(outputWord);
return;
}
}
view.showText.setText("没有此单词");
}
}
}
```
主要的改动在 `WordPolice` 类中,增加了对汉语翻译为英语的处理。在没有找到对应单词时,先按照输入的汉语查找对应的英语单词,如果找到了,就将其翻译为中文输出;如果还没有找到,就按照输入的英语单词查找对应的汉语,如果找到了,就将其翻译为英文输出。如果还是没有找到,就输出提示信息“没有此单词”。
阅读全文