《JAVA课程设计报告-心得体会——计算器、文本编辑器.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计报告-心得体会——计算器、文本编辑器.docx(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、JAVA课程设计报告-心得体会计算器、文本编辑器 用Java设计计算器calculator 内容提要:在本文构造实现了一个计算器擦亮calculator,主要内容包括:calculator 的功能需求分析;calculator的基本设计思路和类的划分;calculator的具体实现。 关键字:Java、计算器calculator 引言:设计实现一个Java应用程序的过程如下: (1)功能需求分析; (2)设计和类划分; (3)代码编写实现。 本文就按照这个步骤来实现计算器calculator的制作。 正文: 1.calculator功能需求分析 作为计算器,至少应该具备以下几点功能: (1)计
2、算器要有GUI界面。 (2)能运算小数,并且不会产生精度损失。 (3)用户可以输入所需计算的数值,可以进行加、减、乘、除四种最基本的运算和混合运算。 (4)允许正负数间的运算。 (5)可以求一个数值的平方及倒数,可以进行阶乘运算。 (6)有菜单栏选项。 具体的功能需求和界面我们可以模仿微软公司出品的windowsXP中自带的计算器。如图一: 图一windows XP 中的计算器界面图 2.calculator基本设计思路和类划分 基于第1节中提出对于calculator功能需求的分析,对这个应用程序设计划分类如下:(1)calculator:这个类的功能是实现一个框架 (2)calculato
3、rFrame:这个类作为主类,实现主要功能:运算,按钮排布,菜单,颜色设置,文本显示 3.calculator的具体实现 3.1 calculator类的设计 calculator用来定义一个计算器的框架 1.主要方法 下面以表格的形式列出calculator类至少应该具有的方法和功能描述(如表一所示)。 表一calculator类的主要方法 方法功能描述 static void main (String args)calculator应用程序的入口,是主方法 3.2 calculatorFrame类的设计 calculatorFrame类实现整体功能,包括窗体的初始化、各种用户事件监听和响应
4、(菜单、运算、结果显示等等)。 1.父类和主要接口 设计calculator整体窗口特性继承自JFrame类。 为了对用户命令做出响应(如帮助菜单栏弹出窗口),calculatorFrame类必须能够监听到用户的命令,因此设计calculatorFrame类实现ActionListener接口。 2.主要方法 下面以表格的形式列出calculatorFrame类至少应该具有的方法和功能描述(如表二所示)。 表二calculatorFrame类的主要方法 3.基本效果 图二为calculator的基本效果图。 4.代码分析 calculator.java代码如下: /calculator.jav
5、a /* *文件名:calculator.java *说明:calculatorFrame主类,实现主要功能*/ /导入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
6、(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 JPane
7、l(); /创建数字键“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); b6=new JButton (4); b6.s
8、etForeground(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 J
9、Button (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.addAction
10、Listener(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=n
11、ew JButton (=); b20.setForeground(Color. YELLOW); b20.addActionListener(this); /”.”显示不清晰,故采用“。”代替 b18=new JButton (。); b18.setForeground(Color. BLUE); b18.addActionListener(this); b10=new JButton (x2); b10.setForeground(Color. YELLOW); b10.addActionListener(this); b15=new JButton (n!); b15.setForegr
12、ound(Color. YELLOW); b15.addActionListener(this); buttonPanel.add(b1); buttonPanel.add(b2); buttonPanel.add(b3); buttonPanel.add(b4); buttonPanel.add(b5); buttonPanel.add(b6); buttonPanel.add(b7); buttonPanel.add(b8); buttonPanel.add(b9); buttonPanel.add(b10); buttonPanel.add(b11); buttonPanel.add(b
13、12); buttonPanel.add(b13); buttonPanel.add(b14); buttonPanel.add(b15); buttonPanel.add(b16); buttonPanel.add(b17); buttonPanel.add(b18); buttonPanel.add(b19); buttonPanel.add(b20); /对按钮面板1进行排版,四行五列buttonPanel.setLayout(new GridLayout(4,5); /建立容纳文本框的面板 JPanel textPanel = new JPanel(); addText = new JTextField(0 , 20); addText.addActionListener(this);