Java实现简易计算器(共15页).doc

上传人:飞****2 文档编号:14235081 上传时间:2022-05-03 格式:DOC 页数:13 大小:97.50KB
返回 下载 相关 举报
Java实现简易计算器(共15页).doc_第1页
第1页 / 共13页
Java实现简易计算器(共15页).doc_第2页
第2页 / 共13页
点击查看更多>>
资源描述

《Java实现简易计算器(共15页).doc》由会员分享,可在线阅读,更多相关《Java实现简易计算器(共15页).doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上Java实训作业 题目: Java实现简易计算器 学 院: 姓 名: 学 号: 班 级: 20 年 月专心-专注-专业一、 实验目的通过课程设计,主要要达到两个目的,一是检验和巩固专业知识、二是提高综合素质和能力。此次课程设计实训主要是Java语言程序设计的实现。通过该课程设计,可以将课堂上掌握的理论知识与处理数据的业务相结合,以检验自己掌握知识的宽度、深度及对知识的综合运用能力。二、 实验要求用Java编写一个简单的计算器,使其能够实现最基本的功能,如简单的加、减、乘、除;平方根,倒数,平方等功能。三、 详细内容1. 界面设计界面设计使用GUI,其中有用到swing

2、组件的TextField和Button,用到awt中的BorderLayout和GridLayout布局管理方式,其图形界面如图1-1所示:图1-1其中主要代码为:public mainWindow()this.setTitle(计算器);/用户图形界面标题this.setVisible(true);/用户图形界面可缩小this.setResizable(false);/用户图形界面不可放大this.setSize(350,300);/设置用户图形界面的大小this.setLocation(400,150);/用户图形界面在屏幕中的显示位置JPanel panel1 = new JPanel(

3、);/新建一个画板JPanel panel2 = new JPanel();button1 = new JButton(1);.reset = new JButton(CE);Container container = this.getContentPane();container.add(panel2,BorderLayout.NORTH);container.add(panel1);panel1.setLayout(new GridLayout(5,4);/将画板1分为4行5列result.setEnabled(false);result.setFont(new Font(Dialog,F

4、ont.BOLD,25);/运算结果的字体大小result.setEditable(false);result.setHorizontalAlignment(SwingConstants.RIGHT);panel1.add(reciprocal);/分别将20个按钮依次添加到画板panel1中,并设置各自的大小reciprocal.setFont(new Font(Dialog,Font.PLAIN,20);.panel1.add(divide);divide.setFont(new Font(Dialog,Font.PLAIN,20);panel2.setLayout(new GridLay

5、out();panel2.add(result);/画板panel2添加运算结果2. 四则运算较为简单的实现了简单的加、减、乘、除运算,主要代码如下:ActionListener equal1 = new ActionListener()/实现四则运算public void actionPerformed(ActionEvent e)String str = result.getText();b = DatatypeConverter.parseDouble(str);if(flag = +)c = a + b;else if(flag = -)c = a - b;else if(flag =

6、 *)c = a * b;else if(flag = / | b != 0)c = a / b;if(flag != =)result.setText( + c);elseresult.setText(零不能做除数!);a = 0;b = 0;c = 0;flag = ;3. 其他功能另外添加了平方根,倒数,平方等功能,主要代码如下:平方根运算的实现:ActionListener sqrt1= new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i

7、 = DatatypeConverter.parseDouble(str);i = Math.sqrt(i);result.setText( + i);倒数运算的实现:ActionListener reciprocal1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = 1/i;result.setText( + i);平方运算的实现:ActionListen

8、er square1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = i*i;result.setText( + i);4. 程序测试经测试发现本计算器基本功能均能实现,可正常运行计算,针对功能实现的代码部分过于简单,可以对其进行改善提高,方便用户使用!5. 实训小结通过对计算器窗体的编写,熟悉了java图形用户界面的设计原理和程序结构,熟悉了java中aw

9、t和swing的组合。学会将书本上的知识运用在实际中,提升了编程能力。四、 源代码import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import jav

10、ax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.xml.bind.DatatypeConverter;public class Calculatorpublic static void main(String args)new mainWindow();/新建主类调用class mainWindow extends JFrameJTextField result = new JTextField(0);/结果栏用于存储运算结果JButton button0;

11、/按钮0JButton button1;/按钮1JButton button2;/按钮2JButton button3;/按钮3JButton button4;/按钮4JButton button5;/按钮5JButton button6;/按钮6JButton button7;/按钮7JButton button8;/按钮8JButton button9;/按钮9JButton reciprocal;/倒数按钮JButton square;/平方按钮JButton sqrt;/平方根按钮JButton reset;/清零按钮JButton add;/加法按钮JButton reduce;/减

12、法按钮JButton multiply;/乘法按钮JButton divide;/除法按钮JButton equal;/等号按钮JButton point;/小数点按钮double a,b,c;String flag;public mainWindow()this.setTitle(计算器);/用户图形界面标题this.setVisible(true);/用户图形界面可缩小this.setResizable(false);/用户图形界面不可放大this.setSize(350,300);/设置用户图形界面的大小this.setLocation(400,150);/用户图形界面在屏幕中的显示位置

13、JPanel panel1 = new JPanel();/新建一个画板JPanel panel2 = new JPanel();button1 = new JButton(1);button2 = new JButton(2);button3 = new JButton(3);button4 = new JButton(4);button5 = new JButton(5);button6 = new JButton(6);button7 = new JButton(7);button8 = new JButton(8);button9 = new JButton(9);button0 =

14、new JButton(0);reciprocal = new JButton(1/X);square = new JButton(X2);sqrt = new JButton();add = new JButton(+);reduce = new JButton(-);multiply = new JButton(*);divide = new JButton(/);equal = new JButton(=);point = new JButton(.);reset = new JButton(CE);Container container = this.getContentPane();

15、container.add(panel2,BorderLayout.NORTH);container.add(panel1);panel1.setLayout(new GridLayout(5,4);/将画板1分为4行5列result.setEnabled(false);result.setFont(new Font(Dialog,Font.BOLD,25);/运算结果的字体大小result.setEditable(false);result.setHorizontalAlignment(SwingConstants.RIGHT);panel1.add(reciprocal);/分别将20个按

16、钮依次添加到画板panel1中,并设置各自的大小reciprocal.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(square);square.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(sqrt);sqrt.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(reset);reset.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button7);button1.setFont(n

17、ew Font(Dialog,Font.PLAIN,20);panel1.add(button8);button2.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button9);button3.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(add);add.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button4);button4.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add

18、(button5);button5.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button6);button6.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(reduce);reduce.setFont(new Font(Dialog,Font.PLAIN,25);panel1.add(button1);button7.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button2);button8.setFont(new Font

19、(Dialog,Font.PLAIN,20);panel1.add(button3);button9.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(multiply);multiply.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(button0);button0.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(point);point.setFont(new Font(Dialog,Font.PLAIN,20);panel1.add(

20、equal);equal.setFont(new Font(Dialog,Font.PLAIN,25);equal.setForeground(Color.red);/将等号设置为红色panel1.add(divide);divide.setFont(new Font(Dialog,Font.PLAIN,20);panel2.setLayout(new GridLayout();panel2.add(result);/画板panel2添加运算结果button0.addActionListener(al0);/设置20个按钮的监听事件button1.addActionListener(al1);

21、button2.addActionListener(al2);button3.addActionListener(al3);button4.addActionListener(al4);button5.addActionListener(al5);button6.addActionListener(al6);button7.addActionListener(al7);button8.addActionListener(al8);button9.addActionListener(al9);reciprocal.addActionListener(reciprocal1);square.add

22、ActionListener(square1);sqrt.addActionListener(sqrt1);reset.addActionListener(reset1);add.addActionListener(add1);point.addActionListener(point1);multiply.addActionListener(multiply1);divide.addActionListener(divide1);equal.addActionListener(equal1);reduce.addActionListener(reduce1);ActionListener a

23、l0 = new ActionListener()/各个按钮的监听事件实现运算public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 0);ActionListener al1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 1);ActionListener al2 = new Act

24、ionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 2);ActionListener al3 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 3);ActionListener al4 = new ActionListener()public void a

25、ctionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 4);ActionListener al5 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 5);ActionListener al6 = new ActionListener()public void actionPerformed(ActionEvent

26、 e)String str = result.getText();result.setText(str + 6);ActionListener al7 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 7);ActionListener al8 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.get

27、Text();result.setText(str + 8);ActionListener al9 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + 9);ActionListener sqrt1= new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = Datatyp

28、eConverter.parseDouble(str);i = Math.sqrt(i);result.setText( + i);ActionListener reciprocal1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = 1/i;result.setText( + i);ActionListener square1 = new ActionLis

29、tener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = i*i;result.setText( + i);ActionListener multiply1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();a = DatatypeConverter.parse

30、Double(str);flag = *;result.setText();ActionListener divide1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();a = DatatypeConverter.parseDouble(str);flag = /;result.setText();ActionListener add1 = new ActionListener()public void actionPerformed(ActionEven

31、t e)String str = result.getText();a = DatatypeConverter.parseDouble(str);flag = +;result.setText();ActionListener reduce1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();a = DatatypeConverter.parseDouble(str);flag = -;result.setText();ActionListener rese

32、t1 = new ActionListener()public void actionPerformed(ActionEvent e)result.setText(0);ActionListener point1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + .);ActionListener equal1 = new ActionListener()/实现四则运算public void actionPerformed(ActionEvent e)String str = result.getText();b = DatatypeConverter.parseDouble(str);if(flag = +)c = a + b;else if(flag = -)c = a - b;else if(flag = *)c = a * b;else if(flag = / | b != 0)c = a / b;if(flag != =)result.setText( + c);elseresult.setText(零不能做除数!);a = 0;b = 0;c = 0;flag = ;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁