课程设计报告
课程:模拟科学计算器
学号:
姓名:
班级:
教师:
时间:10.12.10~11.01.12
计算机科学与技术系
课程设计指导书
姓 名
学 号
班 级
课程名称
JAVA
课程性质
专业必修课
设计时间
设计名称
设计目的
2010 年 12 月 10 日——2011 年 01 月 12 日
模拟科学计算器
巩固所学理论知识,提高程序设计能力的重要实践
环节。综合应用 Java 基础知识和基本方法。
界面模拟 Windows 中的计算器程序。
实现基本数学运算、函数等功能:加、减、乘、除、
设计要求
阶乘、正弦、余弦和指数运算。
实现要点:添加相关组件并进行按钮事件处理。
要求提交 Application 和 Applet 两个版本的程序。
设计 Application 和 Applet 两个版本的程序,对
Application 运用继承 JFrame 设计,类的设计构
造方法利用等。而 Applet 则继承 Applet,在浏览
器上运行。
Application 过程:
1.菜单栏设计.
2.文本域,即为计算器的屏幕显示区域
3.初始化操作
4.统一设置按钮的的使用方式
5.计算器的基础操作(+ - × ÷)
设计思路
与
设计过程
6.运算符号的事件监听
7.清除按钮的事件监听
8.数字输入的事件监听
9.小数点的事件监听
10.main 方法
Applet 过程:
1.init()方法:完成初始化操作;
2.事件统一监听;
3.科学计算;
4.建立 Calucator.html;
5.浏览器运行;
12.10-12.13 复习 Java 程序开发的相关操作,了解“计
算器相关知识”;
12.14-12.17 熟悉题目并理解,及找寻相关资料;
12.18-12.20 根据题目要求进行需求分析设计;
计划与进度
12.21-12.25 对 Application 进行总体程序设计;
12.26-12.30 对 Applet 进行相关各功能的程序设计;
12.31-01.05
01.06-01.10
对程序进行细节完善;
进行调试运行并完成资料归档。
任课教师
意 见
备 注
设计名称:模拟科学计算器
日期:2010 年 01 月 05 日
设计内容:Application 和 Applet 两个版本的程序,实现基本数学运算、函数等
功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。对 Application 运用继
承 JFrame 设计,类的设计构造方法利用等。而 Applet 则继承 Applet,在浏览
器上运行。
设计目的与要求:界面模拟 Windows 中的计算器程序。
实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运
算。
实现要点:添加相关组件并进行按钮事件处理。
要求提交 Application 和 Applet 两个版本的程序。
设计环境或器材、原理与说明:
设计环境或器材: 硬件:计算机
设计原理说明:运用 Application 和 Applet 两个版本的程序。
软件:MyEclipse 集成开发环境 Java JCreator Pro;
设计过程(步骤)或程序代码(可以加页):
Application 程序代码:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class Calucator extends JFrame {
private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;//单选框
private JCheckBoxMenuItem seeItem3;//复选框
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, flag = false;
private String oper = "=";
private double result = 0;
private Num numActionListener;
private DecimalFormat df;
public Calucator(){
super("科学计算器");//设置标题栏
df = new DecimalFormat("#.####");//保留四位小数
this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5 行 6 列
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));
/**
* 菜单栏
*/
myBar = new JMenuBar();
menu1 = new JMenu("编辑(E)");
menu2 = new JMenu("查看(V)");
menu3 = new JMenu("帮助(H)");
menu1.setFont(new Font("宋体", Font.PLAIN, 12));
menu2.setFont(new Font("宋体", Font.PLAIN, 12));
menu3.setFont(new Font("宋体", Font.PLAIN, 12));
/**
* 编辑栏
*/
editItem1 = new JMenuItem("复制(C) Ctrl+C");
editItem2 = new JMenuItem("粘贴(P) Ctrl+V");
editItem1.setFont(new Font("宋体",Font.PLAIN,12));
editItem2.setFont(new Font("宋体",Font.PLAIN,12));
/**
* 查看栏
*/
seeItem1 = new JRadioButtonMenuItem("科学型(T)");
seeItem2 = new JRadioButtonMenuItem("标准型(S)");
seeItem3 = new JCheckBoxMenuItem("数字分组(I)");
seeItem1.setFont(new Font("宋体",Font.PLAIN,12));
seeItem2.setFont(new Font("宋体",Font.PLAIN,12));
seeItem3.setFont(new Font("宋体",Font.PLAIN,12));
/**
* 帮助栏
*/
help1 = new JMenuItem("帮助主题(H)");
help2 = new JMenuItem("关于计算器(A)");
help1.setFont(new Font("宋体",Font.PLAIN,12));
help2.setFont(new Font("宋体",Font.PLAIN,12));
bgb = new ButtonGroup();//选项组
menu1.add(editItem1);
menu1.add(editItem2);
menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();//添加一条分割线
menu2.add(seeItem3);
menu3.add(help1);
menu3.addSeparator();//添加一条分割线
menu3.add(help2);
myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);
this.setJMenuBar(myBar);
numActionListener = new Num();//实现数字监听
/**
* 文本域,即为计算器的屏幕显示区域
*/
tf = new JTextField();
tf.setEditable(false);//文本区域不可编辑
tf.setBackground(Color.white);//文本区域的背景色
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
tf.setText("0");
tf.setBorder(BorderFactory.createLoweredBevelBorder());
init();//对计算器进行初始化
}
/**
* 初始化操作
* 添加按钮
*/
private void init(){
addButton(panel1, "Backspace", new Clear(), Color.red);
addButton(panel1, "CE", new Clear(), Color.red);
addButton(panel1, "C", new Clear(), Color.red);
addButton(panel2, "1/x", new Signs(), Color.magenta);
addButton(panel2, "log", new Signs(), Color.magenta);
addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "÷", new Signs(), Color.red);
addButton(panel2, "n!", new Signs(), Color.magenta);
addButton(panel2, "sqrt", new Signs(), Color.magenta);
addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "×", new Signs(), Color.red);
addButton(panel2, "sin", new Signs(), Color.magenta);
addButton(panel2, "x^2", new Signs(), Color.magenta);
addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new Signs(), Color.red);
addButton(panel2, "cos", new Signs(), Color.magenta);
addButton(panel2, "x^3", new Signs(), Color.magenta);
addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new Clear(), Color.blue);
addButton(panel2, ".", new Dot(), Color.blue);
addButton(panel2, "+", new Signs(), Color.red);
addButton(panel2, "tan", new Signs(), Color.magenta);
addButton(panel2, "%", new Signs(), Color.magenta);
addButton(panel2, "π", numActionListener, Color.orange);
addButton(panel2, "e", numActionListener, Color.orange);
addButton(panel2, "′″", new Signs(), Color.orange);
addButton(panel2, "=", new Signs(), Color.red);
JButton btns = new JButton("计算器");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);//按钮不可操作
btns.setPreferredSize(new Dimension(20, 20));
panel3.add(btns);//加入按钮
addButton(panel3, "MC", null, Color.red);
addButton(panel3, "MR", null, Color.red);
addButton(panel3, "MS", null, Color.red);
addButton(panel3, "M+", null, Color.red);
panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);
pack();
this.setResizable(false);//窗口不可改变大小
this.setLocation(300, 200);