没有合适的资源?快使用搜索试试~ 我知道了~
JAVA课程设计报告心得体会计算器文本编辑器.docx
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
41 浏览量
2022-07-08
06:01:06
上传
评论
收藏 718KB DOCX 举报
温馨提示
JAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docxJAVA课程设计报告心得体会计算器文本编辑器.docx
资源推荐
资源详情
资源评论




















用 Java 设计计算器 calculator
内容提要:在本文构造实现了一个计算器擦亮 calculator,主要内容包括:calculator 的
功能需求分析;calculator 的基本设计思路和类的划分;calculator 的具体实现。
关键字:Java、计算器 calculator
引言:设计实现一个 Java 应用程序的过程如下:
(1) 功能需求分析;
(2) 设计和类划分;
(3) 代码编写实现。
本文就按照这个步骤来实现计算器 calculator 的制作。
正文:
1. calculator 功能需求分析
作为计算器,至少应该具备以下几点功能:
(1) 计算器要有 GUI 界面。
(2) 能运算小数,并且不会产生精度损失。
(3) 用户可以输入所需计算的数值,可以进行加、减、乘、除四种最基本的运算和
混合运算。
(4) 允许正负数间的运算。
(5) 可以求一个数值的平方及倒数,可以进行阶乘运算。
(6) 有菜单栏选项。
具体的功能需求和界面我们可以模仿微软公司出品的 windowsXP 中自带的计算器。如
图一:
图一 windows XP 中的计算器界面图
2. calculator 基本设计思路和类划分
基于第 1 节中提出对于 calculator 功能需求的分析,对这个应用程序设计划分类如下:
(1) calculator:这个类的功能是实现一个框架
(2) calculatorFrame:这个类作为主类,实现主要功能:运算,按钮排布,菜单,颜
色设置,文本显示
3. calculator 的具体实现
1 / 24

3.1 calculator 类的设计
calculator 用来定义一个计算器的框架
1.主要方法
下面以表格的形式列出 calculator 类至少应该具有的方法和功能描述(如表一所示)。
表一 calculator 类的主要方法
方法
功能描述
static void main (String args[])
calculator 应用程序的入口,是主方法
3.2 calculatorFrame 类的设计
calculatorFrame 类实现整体功能,包括窗体的初始化、各种用户事件监听和响应(菜
单、运算、结果显示等等)。
1. 父类和主要接口
设计 calculator 整体 窗口特性继承自 JFrame 类。
为了对用户命令做出响应(如帮助菜单栏弹出窗口),calculatorFrame 类必须能够
监听到用户的命令,因此设计 calculatorFrame 类实现 ActionListener 接口。
2. 主要方法
下面以表格的形式列出 calculatorFrame 类至少应该具有的方法和功能描述(如表二
所示)。
表二 calculatorFrame 类的主要方法
方法
重载 ActionListener 接口中的方法,用于对用
户命令进行响应,用户命令包括“帮助”“版
权”“说明”以及各个按钮等
void actionPerformed(ActionEvent
e)
3. 基本效果
图二为 calculator 的基本效果图。
2 / 24

4. 代码分析
calculator.java代码如下:
//calculator.java
/*
*文件名:calculator.java
*说明:calculatorFrame 主类,实现主要功能
*/
3 / 24

//导入AWT包
import java.awt.*;
import java.awt.event.*;
//导入 SWING 包
import javax.swing.*;
import javax.swing.event.*;
class calculator
{
public static void main(String[] args)
{
calculatorFrame frame = new calculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
//主类calculatorFrame
class calculatorFrame extends JFrame
implements ActionListener
{
public calculatorFrame()
{
//设置框架主题及大小
setTitle("计算器");
setSize(300,235);
//将面板置于框架中
Container contentPane = getContentPane();
setResizable(false);
//创建按钮面板
JPanel buttonPanel = new JPanel();
//创建数字键“1”
b11=new JButton ("1");
//设置颜色
b11.setForeground(Color. BLUE);
//创建事件监听器
b11.addActionListener(this);
b12=new JButton ("2");
b12.setForeground(Color. BLUE);
b12.addActionListener(this);
b13=new JButton ("3");
b13.setForeground(Color. BLUE);
b13.addActionListener(this);
4 / 24

b6=new JButton ("4");
b6.setForeground(Color. BLUE);
b6.addActionListener(this);
b7=new JButton ("5");
b7.setForeground(Color. BLUE);
b7.addActionListener(this);
b8=new JButton ("6");
b8.setForeground(Color. BLUE);
b8.addActionListener(this);
b1=new JButton ("7");
b1.setForeground(Color. BLUE);
b1.addActionListener(this);
b2=new JButton ("8");
b2.setForeground(Color. BLUE);
b2.addActionListener(this);
b3=new JButton ("9");
b3.setForeground(Color. BLUE);
b3.addActionListener(this);
b16=new JButton ("0");
b16.setForeground(Color. BLUE);
b16.addActionListener(this);
b17=new JButton ("+/-");
b17.setForeground(Color. BLUE);
b17.addActionListener(this);
b4=new JButton ("+");
b4.addActionListener(this);
b9=new JButton ("-");
b9.addActionListener(this);
b14=new JButton ("*");
b14.addActionListener(this);
b19=new JButton ("/");
b19.addActionListener(this);
b5=new JButton ("1/x");
b5.setForeground(Color. YELLOW);
b5.addActionListener(this);
b20=new JButton ("=");
b20.setForeground(Color. YELLOW);
b20.addActionListener(this);
//”.”显示不清晰,故采用“。”代替
b18=new JButton ("。");
b18.setForeground(Color. BLUE);
b18.addActionListener(this);
b10=new JButton ("x^2");
5 / 24
剩余23页未读,继续阅读
资源评论


春哥111
- 粉丝: 1w+
- 资源: 5万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
