Java实现简易计算器_第1页
Java实现简易计算器_第2页
Java实现简易计算器_第3页
Java实现简易计算器_第4页
Java实现简易计算器_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、到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(ne

4、w Font("Dialog",Font.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("Dialo

5、g",Font.PLAIN,20);panel2.setLayout(new GridLayout();panel2.add(result);/画板panel2添加运算结果2. 四则运算较为简单的实现了简单的加、减、乘、除运算,主要代码如下:ActionListener equal1 = new ActionListener()/实现四则运算public void actionPerformed(ActionEvent e)String str = result.getText();b = DatatypeConverter.parseDouble(str);if(flag = &q

6、uot;+")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 = ""3. 其他功能另外添加了平方根,倒

7、数,平方等功能,主要代码如下:平方根运算的实现:ActionListener sqrt1= new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = Math.sqrt(i);result.setText("" + i);倒数运算的实现:ActionListener reciprocal1 = new ActionListener()public vo

8、id actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = 1/i;result.setText("" + i);平方运算的实现:ActionListener square1 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConver

9、ter.parseDouble(str);i = i*i;result.setText("" + i);4. 程序测试经测试发现本计算器基本功能均能实现,可正常运行计算,针对功能实现的代码部分过于简单,可以对其进行改善提高,方便用户使用!5. 实训小结通过对计算器窗体的编写,熟悉了java图形用户界面的设计原理和程序结构,熟悉了java中awt和swing的组合。学会将书本上的知识运用在实际中,提升了编程能力。四、 源代码import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;

10、import java.awt.Font;import java.awt.GridLayout;importent.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.xml.bind.DatatypeConverter;public cla

11、ss Calculatorpublic static void main(String args)new mainWindow();/新建主类调用class mainWindow extends JFrameJTextField result = new JTextField("0");/结果栏用于存储运算结果JButton button0;/按钮0JButton button1;/按钮1JButton button2;/按钮2JButton button3;/按钮3JButton button4;/按钮4JButton button5;/按钮5JButton button

12、6;/按钮6JButton button7;/按钮7JButton button8;/按钮8JButton button9;/按钮9JButton reciprocal;/倒数按钮JButton square;/平方按钮JButton sqrt;/平方根按钮JButton reset;/清零按钮JButton add;/加法按钮JButton reduce;/减法按钮JButton multiply;/乘法按钮JButton divide;/除法按钮JButton equal;/等号按钮JButton point;/小数点按钮double a,b,c;String flag;public ma

13、inWindow()this.setTitle("计算器");/用户图形界面标题this.setVisible(true);/用户图形界面可缩小this.setResizable(false);/用户图形界面不可放大this.setSize(350,300);/设置用户图形界面的大小this.setLocation(400,150);/用户图形界面在屏幕中的显示位置JPanel panel1 = new JPanel();/新建一个画板JPanel panel2 = new JPanel();button1 = new JButton("1");butt

14、on2 = 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

15、= 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 JBut

16、ton("=");point = new JButton(".");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("

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

18、LAIN,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(new Font("Dialog",Font.PLAIN,20);panel1.add(button8);button2.setFont(new Font("Dialog",

19、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(button5);button5.setFont(new Font("Dialo

20、g",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 Fo

21、nt("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.

22、setFont(new Font("Dialog",Font.PLAIN,20);panel1.add(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);/画板p

23、anel2添加运算结果button0.addActionListener(al0);/设置20个按钮的监听事件button1.addActionListener(al1);button2.addActionListener(al2);button3.addActionListener(al3);button4.addActionListener(al4);button5.addActionListener(al5);button6.addActionListener(al6);button7.addActionListener(al7);button8.addActionListener(al

24、8);button9.addActionListener(al9);reciprocal.addActionListener(reciprocal1);square.addActionListener(square1);sqrt.addActionListener(sqrt1);reset.addActionListener(reset1);add.addActionListener(add1);point.addActionListener(point1);multiply.addActionListener(multiply1);divide.addActionListener(divid

25、e1);equal.addActionListener(equal1);reduce.addActionListener(reduce1);ActionListener al0 = new ActionListener()/各个按钮的监听事件实现运算public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + "0");ActionListener al1 = new ActionListener()public void actionPerforme

26、d(ActionEvent e)String str = result.getText();result.setText(str + "1");ActionListener al2 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + "2");ActionListener al3 = new ActionListener()public void actionPerformed(

27、ActionEvent e)String str = result.getText();result.setText(str + "3");ActionListener al4 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + "4");ActionListener al5 = new ActionListener()public void actionPerformed(Ac

28、tionEvent e)String str = result.getText();result.setText(str + "5");ActionListener al6 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + "6");ActionListener al7 = new ActionListener()public void actionPerformed(Acti

29、onEvent e)String str = result.getText();result.setText(str + "7");ActionListener al8 = new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();result.setText(str + "8");ActionListener al9 = new ActionListener()public void actionPerformed(Action

30、Event e)String str = result.getText();result.setText(str + "9");ActionListener sqrt1= new ActionListener()public void actionPerformed(ActionEvent e)String str = result.getText();double i = DatatypeConverter.parseDouble(str);i = Math.sqrt(i);result.setText("" + i);ActionListener r

31、eciprocal1 = 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 ActionListener()public void actionPerformed(ActionEvent e)String str = result.g

32、etText();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.parseDouble(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 ActionListen

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论