请用Java语言运用JfreeChart等方法编写完整的代码实现具有GUI基本功能的口算练习程序,该程序能够生成Word格式的习题 ;然后批改习题并存储用户得分;最后实现习题得分的可视化展示

时间: 2024-02-25 22:59:02 浏览: 23
好的,下面是一个完整的Java代码示例,用于实现口算练习程序的基本功能,并生成Word格式的习题,批改习题并存储用户得分,最后实现习题得分的可视化展示。请注意,该程序需要使用JFreeChart等相关库文件,您需要自行下载并配置好Java环境。 ```java import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; public class MathExercise { private JFrame frame; private JTextField num1Field; private JTextField num2Field; private JTextField resultField; private JButton startButton; private JButton checkButton; private JButton saveButton; private JLabel scoreLabel; private int score = 0; private List<String> exercises = new ArrayList<>(); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MathExercise window = new MathExercise(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public MathExercise() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 500, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); frame.getContentPane().add(panel, BorderLayout.CENTER); panel.setLayout(null); JLabel num1Label = new JLabel("数字1:"); num1Label.setBounds(50, 50, 80, 20); panel.add(num1Label); num1Field = new JTextField(); num1Field.setEditable(false); num1Field.setBounds(140, 50, 80, 20); panel.add(num1Field); num1Field.setColumns(10); JLabel num2Label = new JLabel("数字2:"); num2Label.setBounds(250, 50, 80, 20); panel.add(num2Label); num2Field = new JTextField(); num2Field.setEditable(false); num2Field.setBounds(340, 50, 80, 20); panel.add(num2Field); num2Field.setColumns(10); JLabel resultLabel = new JLabel("结果:"); resultLabel.setBounds(50, 100, 80, 20); panel.add(resultLabel); resultField = new JTextField(); resultField.setBounds(140, 100, 80, 20); panel.add(resultField); resultField.setColumns(10); startButton = new JButton("开始"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { start(); } }); startButton.setBounds(50, 150, 80, 20); panel.add(startButton); checkButton = new JButton("检查"); checkButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { check(); } }); checkButton.setEnabled(false); checkButton.setBounds(140, 150, 80, 20); panel.add(checkButton); saveButton = new JButton("保存"); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { save(); } }); saveButton.setEnabled(false); saveButton.setBounds(230, 150, 80, 20); panel.add(saveButton); scoreLabel = new JLabel("得分:0"); scoreLabel.setBounds(50, 200, 80, 20); panel.add(scoreLabel); } private void start() { Random random = new Random(); int num1 = random.nextInt(100); int num2 = random.nextInt(100); int result = num1 + num2; String exercise = num1 + " + " + num2 + " = "; num1Field.setText(String.valueOf(num1)); num2Field.setText(String.valueOf(num2)); resultField.setText(""); exercises.add(exercise); startButton.setEnabled(false); checkButton.setEnabled(true); saveButton.setEnabled(true); } private void check() { String resultStr = resultField.getText(); if (resultStr.equals("")) { return; } int result = Integer.parseInt(resultStr); int num1 = Integer.parseInt(num1Field.getText()); int num2 = Integer.parseInt(num2Field.getText()); int expected = num1 + num2; if (result == expected) { score++; } scoreLabel.setText("得分:" + score); startButton.setEnabled(true); checkButton.setEnabled(false); saveButton.setEnabled(false); } private void save() { try { XWPFDocument doc = new XWPFDocument(); for (String exercise : exercises) { XWPFParagraph p = doc.createParagraph(); XWPFRun r = p.createRun(); r.setText(exercise); } File file = new File("exercise.docx"); FileOutputStream out = new FileOutputStream(file); doc.write(out); out.close(); doc.close(); System.out.println("习题已保存为" + file.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } private void showChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(score, "得分", ""); JFreeChart chart = ChartFactory.createBarChart("习题得分", "", "得分", dataset, PlotOrientation.VERTICAL, false, false, false); ChartPanel chartPanel = new ChartPanel(chart); JFrame chartFrame = new JFrame("习题得分"); chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); chartFrame.getContentPane().add(chartPanel, BorderLayout.CENTER); chartFrame.pack(); chartFrame.setVisible(true); } } ``` 该程序采用了Swing编写GUI,可以生成加法习题,并在GUI上进行口算练习,批改答案并存储得分,最后可视化展示习题得分。您可以根据需要进行修改和完善,以满足您的具体需求。

相关推荐

最新推荐

recommend-type

纯Java动态生成SVG饼图与JFreeChart超强功能生成SVG图表

纯Java动态生成SVG饼图与JFreeChart超强功能生成SVG图表
recommend-type

Java程序中用JFreeChart制作图形报表

Java程序中用JFreeChart制作图形报表
recommend-type

使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP

虽然现在JS做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西! 这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下!
recommend-type

.各种基于 Go 语言实现的限流算法集合。.zip

Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
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取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
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。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依