用java写一个背单词的程序,下面是要求 界面可以再好看一点;可以去掉音标;可以改变单词显示的速度;可以增加标记生词并记到生词本中;可以增加测试的功能(单词含义可以随机选4个词的含义来让用户选择)等等。附件中有两个代码,一个单词本
时间: 2024-02-01 20:16:02 浏览: 72
java背单词程序
很好,下面是我的实现思路:
1. 设计界面:使用Java Swing库来设计界面,可以添加背景图片、设置字体和颜色等来美化界面。
2. 读取单词表:从给定的单词本文件中读取单词和对应的含义,存储到程序中。
3. 显示单词:根据用户设置的速度,逐个显示单词,并提供一个按钮来暂停和继续显示。
4. 生词本:在单词显示的过程中,用户可以点击一个按钮来将当前单词标记为生词,然后将其保存到生词本中。
5. 测试功能:提供一个测试功能,随机选取一个单词和三个错误的含义,让用户选择正确的含义。
下面是代码的实现:
1. 界面设计
```java
import javax.swing.*;
import java.awt.*;
public class VocabularyApp extends JFrame {
private JLabel wordLabel;
private JLabel meaningLabel;
private JButton pauseButton;
private JButton addWordButton;
private JButton testButton;
private JPanel contentPane;
public VocabularyApp() {
setTitle("背单词");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 300);
setResizable(false);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
// 背景图片
ImageIcon bgIcon = new ImageIcon("bg.jpg");
JLabel bgLabel = new JLabel(bgIcon);
bgLabel.setBounds(0, 0, bgIcon.getIconWidth(), bgIcon.getIconHeight());
contentPane.add(bgLabel);
// 单词标签
wordLabel = new JLabel();
wordLabel.setBounds(50, 50, 400, 50);
wordLabel.setForeground(Color.WHITE);
wordLabel.setFont(new Font("微软雅黑", Font.BOLD, 36));
bgLabel.add(wordLabel);
// 含义标签
meaningLabel = new JLabel();
meaningLabel.setBounds(50, 120, 400, 30);
meaningLabel.setForeground(Color.WHITE);
meaningLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
bgLabel.add(meaningLabel);
// 暂停按钮
pauseButton = new JButton("暂停");
pauseButton.setBounds(200, 200, 80, 30);
bgLabel.add(pauseButton);
// 生词本按钮
addWordButton = new JButton("生词本");
addWordButton.setBounds(300, 200, 80, 30);
bgLabel.add(addWordButton);
// 测试按钮
testButton = new JButton("测试");
testButton.setBounds(400, 200, 80, 30);
bgLabel.add(testButton);
}
public void setWord(String word) {
wordLabel.setText(word);
}
public void setMeaning(String meaning) {
meaningLabel.setText(meaning);
}
public void setPauseButtonText(String text) {
pauseButton.setText(text);
}
public void setAddWordButtonText(String text) {
addWordButton.setText(text);
}
public void setTestButtonText(String text) {
testButton.setText(text);
}
public JButton getPauseButton() {
return pauseButton;
}
public JButton getAddWordButton() {
return addWordButton;
}
public JButton getTestButton() {
return testButton;
}
}
```
2. 读取单词表
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Vocabulary {
private List<String> words;
private List<String> meanings;
private int currentIndex;
public Vocabulary(String filename) {
words = new ArrayList<>();
meanings = new ArrayList<>();
currentIndex = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\t");
words.add(parts[0]);
meanings.add(parts[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public String getCurrentWord() {
return words.get(currentIndex);
}
public String getCurrentMeaning() {
return meanings.get(currentIndex);
}
public void nextWord() {
currentIndex = (currentIndex + 1) % words.size();
}
public void prevWord() {
currentIndex = (currentIndex - 1 + words.size()) % words.size();
}
public void reset() {
currentIndex = 0;
}
public int getSize() {
return words.size();
}
public void addWord(String word, String meaning) {
words.add(word);
meanings.add(meaning);
}
public void removeWord(int index) {
if (index >= 0 && index < words.size()) {
words.remove(index);
meanings.remove(index);
}
}
public void shuffle() {
for (int i = words.size() - 1; i > 0; i--) {
int j = (int) (Math.random() * (i + 1));
String temp1 = words.get(i);
String temp2 = meanings.get(i);
words.set(i, words.get(j));
meanings.set(i, meanings.get(j));
words.set(j, temp1);
meanings.set(j, temp2);
}
}
}
```
3. 显示单词
```java
import javax.swing.*;
public class VocabularyAppController {
private VocabularyApp view;
private Vocabulary model;
private boolean paused;
private int speed;
public VocabularyAppController() {
view = new VocabularyApp();
model = new Vocabulary("words.txt");
paused = false;
speed = 1;
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
view.getPauseButton().addActionListener(e -> {
if (paused) {
view.setPauseButtonText("暂停");
paused = false;
} else {
view.setPauseButtonText("继续");
paused = true;
}
});
view.getAddWordButton().addActionListener(e -> {
JOptionPane.showMessageDialog(view, "已加入生词本");
});
view.getTestButton().addActionListener(e -> {
JOptionPane.showMessageDialog(view, "TODO");
});
new Thread(() -> {
while (true) {
if (!paused) {
model.nextWord();
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
}
try {
Thread.sleep(1000 / speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
view.setVisible(true);
}
}
```
4. 生词本
```java
import javax.swing.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class VocabularyAppController {
private VocabularyApp view;
private Vocabulary model;
private boolean paused;
private int speed;
private boolean[] newWords;
public VocabularyAppController() {
view = new VocabularyApp();
model = new Vocabulary("words.txt");
paused = false;
speed = 1;
newWords = new boolean[model.getSize()];
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
view.getPauseButton().addActionListener(e -> {
if (paused) {
view.setPauseButtonText("暂停");
paused = false;
} else {
view.setPauseButtonText("继续");
paused = true;
}
});
view.getAddWordButton().addActionListener(e -> {
int index = model.getCurrentIndex();
newWords[index] = true;
try (BufferedWriter writer = new BufferedWriter(new FileWriter("new_words.txt", true))) {
writer.write(model.getCurrentWord() + "\t" + model.getCurrentMeaning() + "\n");
} catch (IOException ex) {
ex.printStackTrace();
}
view.setAddWordButtonText("已加入生词本");
});
view.getTestButton().addActionListener(e -> {
JOptionPane.showMessageDialog(view, "TODO");
});
new Thread(() -> {
while (true) {
if (!paused) {
model.nextWord();
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
}
try {
Thread.sleep(1000 / speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
view.setVisible(true);
}
}
```
5. 测试功能
```java
import javax.swing.*;
public class VocabularyAppController {
private VocabularyApp view;
private Vocabulary model;
private boolean paused;
private int speed;
private boolean[] newWords;
public VocabularyAppController() {
view = new VocabularyApp();
model = new Vocabulary("words.txt");
paused = false;
speed = 1;
newWords = new boolean[model.getSize()];
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
view.getPauseButton().addActionListener(e -> {
if (paused) {
view.setPauseButtonText("暂停");
paused = false;
} else {
view.setPauseButtonText("继续");
paused = true;
}
});
view.getAddWordButton().addActionListener(e -> {
int index = model.getCurrentIndex();
newWords[index] = true;
try (BufferedWriter writer = new BufferedWriter(new FileWriter("new_words.txt", true))) {
writer.write(model.getCurrentWord() + "\t" + model.getCurrentMeaning() + "\n");
} catch (IOException ex) {
ex.printStackTrace();
}
view.setAddWordButtonText("已加入生词本");
});
view.getTestButton().addActionListener(e -> {
String word = model.getCurrentWord();
String[] options = new String[4];
options[0] = model.getCurrentMeaning();
for (int i = 1; i < 4; i++) {
int index;
do {
index = (int) (Math.random() * model.getSize());
} while (index == model.getCurrentIndex());
options[i] = model.getMeaning(index);
}
int result = JOptionPane.showOptionDialog(view, "请选择 " + word + " 的含义:",
"测试", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (result == 0) {
JOptionPane.showMessageDialog(view, "回答正确!");
} else {
JOptionPane.showMessageDialog(view, "回答错误!");
}
});
new Thread(() -> {
while (true) {
if (!paused) {
model.nextWord();
view.setWord(model.getCurrentWord());
view.setMeaning(model.getCurrentMeaning());
}
try {
Thread.sleep(1000 / speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
view.setVisible(true);
}
}
```
最后,在main方法中创建一个VocabularyAppController对象,即可运行程序。
阅读全文