java图形化界面英语词典
时间: 2023-11-20 17:58:49 浏览: 60
以下是一个基于Java Swing实现的英语词典的例子,它包括英译中和中译英两种翻译方式,以及生词本功能,用户可以将生词保存起来。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Dictionary extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel label;
private JTextField textField;
private JTextArea textArea;
private JButton button1, button2, button3, button4;
private JScrollPane scrollPane;
public Dictionary() {
setTitle("英语词典");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null);
label = new JLabel("请输入单词:");
textField = new JTextField(20);
textArea = new JTextArea(15, 30);
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
button1 = new JButton("英译中");
button2 = new JButton("中译英");
button3 = new JButton("添加生词");
button4 = new JButton("查看生词本");
JPanel panel1 = new JPanel();
panel1.add(label);
panel1.add(textField);
JPanel panel2 = new JPanel();
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
setLayout(new BorderLayout());
add(panel1, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String word = textField.getText();
String meaning = getMeaning(word, "english_chinese.txt");
textArea.setText(meaning);
} else if (e.getSource() == button2) {
String word = textField.getText();
String meaning = getMeaning(word, "chinese_english.txt");
textArea.setText(meaning);
} else if (e.getSource() == button3) {
String word = textField.getText();
addWord(word, "new_words.txt");
JOptionPane.showMessageDialog(this, "添加成功!");
} else if (e.getSource() == button4) {
String words = getWords("new_words.txt");
textArea.setText(words);
}
}
public String getMeaning(String word, String fileName) {
String meaning = "";
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null) {
String[] parts = line.split("\t");
if (parts[0].equalsIgnoreCase(word)) {
meaning = parts[1];
break;
}
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (meaning.equals("")) {
meaning = "未找到该单词!";
}
return meaning;
}
public void addWord(String word, String fileName) {
try {
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(word);
bw.newLine();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getWords(String fileName) {
String words = "";
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null) {
words += line + "\n";
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (words.equals("")) {
words = "生词本为空!";
}
return words;
}
public static void main(String[] args) {
Dictionary dict = new Dictionary();
dict.setVisible(true);
}
}
```