用java写一个背单词的程序,下面是要求 界面可以再好看一点;可以去掉音标;可以改变单词显示的速度;可以增加标记生词并记到生词本中;可以增加测试的功能(单词含义可以随机选4个词的含义来让用户选择)等等。附件中有两个代码,一个单词本

时间: 2024-02-01 14:16:02 浏览: 29
很好,下面是我的实现思路: 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对象,即可运行程序。

相关推荐

最新推荐

recommend-type

最最常用java编程中英文对照表 带音标

最最常用java编程中英文对照表 带音标 最最常用java编程中英文对照表 带音标最最常用java编程中英文对照表 带音标
recommend-type

1800个程序员必备词汇-开发必备-适用前后端-编程词汇-1800词40页高清完整版-带音标-右侧下载前可预览.pdf

1800个程序员必备词汇,本词汇汇集了前后端软件开发中常用词汇,同时带有音标,基本满足日常开发需求,适合编程初学者及各阶段开发者学习使用。
recommend-type

JAVA最常用的英语单词

JAVA最常用的英语单词!JAVA最常用的英语单词! JAVA最常用的英语单词1
recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依