java编写的加法计算器
用java代码写的简易计算器(可以实现基本的加减乘除功能)
⽤java代码写的简易计算器(可以实现基本的加减乘除功能)package A;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;public class Calcular3 extends JFrame implements ActionListener,MouseListener{private int m1=0,n=0;//private double m2=0;//运算的数private int flag=0;JFrame f;JPanel p1,p2,p3;JTextField t;JButton b1[]=new JButton[18];String b[]= {"1","2","3","4","5","6","7","8","9","0","清空","退格",".","=","+","-","*","/"};public Calcular3(){f=new JFrame("计算器");t=new JTextField(35);p1=new JPanel();p2=new JPanel();p3=new JPanel();f.setBounds(100, 100, 400, 200);f.add(p1,BorderLayout.NORTH);f.add(p2,BorderLayout.CENTER);f.add(p3,BorderLayout.EAST);p2.setLayout(new GridLayout(5,3));p3.setLayout(new GridLayout(4,1));p1.add(t);for(int i=0;i<14;i++) {b1[i]=new JButton(b[i]);p2.add(b1[i]);b1[i].addActionListener(this);}for(int i=14;i<18;i++) {b1[i]=new JButton(b[i]);p3.add(b1[i]);b1[i].addActionListener(this);}/*for(int i=0;i<18;i++) {b1[i].addActionListener(this);}*/f.setVisible(true);}//实现接⼝的⽅法public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void actionPerformed(ActionEvent e) {String str="";int i;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {if(i==9) {n=n*10;}else {n=n*10+i+1;}str=String.valueOf(n);//整形n转换成字符串strt.setText(str);//显⽰到⽂本框上}}for(i=14;i<18;i++) {//+、-、*、/if(e.getSource()==b1[i]) {//匹配运算符m1=Integer.parseInt(t.getText());if(flag==15) {m2=m1+m2;}else if(flag==16) {m2=m1-m2;}else if(flag==17) {m2=m1*m2;}else if(flag==18) {m2=m1/m2;}else m2=m1;//若⽆连续的运算符运算,保存当前数据到m2 if(i==14) flag=15;else if(i==15) flag=16;else if(i==16) flag=17;else flag=18;str=String.valueOf(b[i]);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据break;//找到匹配数据退出循环}}if(e.getSource()==b1[13]) {//=m1=Integer.parseInt(t.getText());if(flag==15) {m2=m2+m1;}else if(flag==16) {m2=m2-m1;}else if(flag==17) {m2=m2*m1;}else if(flag==18) {m2=m2/m1;}else m2=m1;str=String.valueOf(m2);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据flag=0;//flag还原0,表明没有未处理的运算符}if(e.getSource()==b1[10]) {//各变量变为0 清空m1=0;m2=0;flag=0;n=0;t.setText("0");//显⽰到⽂本框上}if(e.getSource()==b1[11]) {//退格m1=(int)(Double.parseDouble(t.getText())/10);n=m1;str=String.valueOf(m1);t.setText(str);}if(e.getSource()==b1[12]) {//⼩数点m1=Integer.parseInt(t.getText());str=String.valueOf(m1+b[12]);t.setText(str);//显⽰到⽂本框上int j=0;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {j++;m2=Math.pow(0.1, j)*Integer.parseInt(b[i]);str=String.valueOf(m1+m2);t.setText(str);//显⽰到⽂本框上}}}}//主函数public static void main(String[] args) {new Calcular3();}}。
Java实现简易计算器
Java实训作业题目:Java实现简易计算器学院:姓名:学号:班级:20 年月一、实验目的通过课程设计,主要要达到两个目的,一是检验和巩固专业知识、二是提高综合素质和能力。
此次课程设计实训主要是Java语言程序设计的实现。
通过该课程设计,可以将课堂上掌握的理论知识与处理数据的业务相结合,以检验自己掌握知识的宽度、深度及对知识的综合运用能力。
二、实验要求用Java编写一个简单的计算器,使其能够实现最基本的功能,如简单的加、减、乘、除;平方根,倒数,平方等功能。
三、详细内容1.界面设计界面设计使用GUI,其中有用到swing组件的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();//新建一个画板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",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("Dialog",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 == "+")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.其他功能另外添加了平方根,倒数,平方等功能,主要代码如下:平方根运算的实现: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 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.getText();double i = DatatypeConverter.parseDouble(str);i = i*i;result.setText("" + i);}};4.程序测试经测试发现本计算器基本功能均能实现,可正常运行计算,针对功能实现的代码部分过于简单,可以对其进行改善提高,方便用户使用!5.实训小结通过对计算器窗体的编写,熟悉了java图形用户界面的设计原理和程序结构,熟悉了java中awt和swing的组合。
java计算器设计实验报告
java计算器设计实验报告《Java计算器设计实验报告》摘要:本实验报告基于Java语言,设计了一个简单的计算器。
通过使用面向对象的编程方法,实现了基本的加、减、乘、除运算,并且实现了用户界面的交互操作。
本实验报告详细介绍了计算器的设计思路、实现过程和代码结构,并对实验结果进行了分析和总结。
1. 引言计算器是人们日常生活中常用的工具之一,它能够帮助人们进行简单的数学运算。
随着计算器的普及和发展,计算器的功能也越来越丰富,例如科学计算器、金融计算器等。
本实验旨在通过使用Java语言,设计一个简单的计算器,以加深对面向对象编程的理解和应用。
2. 设计思路在设计计算器时,首先需要确定计算器的功能需求,包括加法、减法、乘法、除法等基本运算。
其次,需要考虑用户界面的设计,使用户能够方便地输入数字和选择运算符。
最后,需要考虑计算器的实现方式,包括数据结构的选择、算法的设计等。
3. 实现过程在本实验中,我们使用Java语言和Swing库来实现计算器的设计。
首先,我们创建一个Calculator类来处理计算器的逻辑操作,包括加法、减法、乘法、除法等运算。
然后,我们创建一个CalculatorUI类来处理用户界面的设计,包括数字按钮、运算符按钮和显示屏的设计。
最后,我们将Calculator类和CalculatorUI类进行整合,实现一个完整的计算器。
4. 代码结构以下是计算器的主要代码结构:Calculator类:- add(double a, double b):加法运算- subtract(double a, double b):减法运算- multiply(double a, double b):乘法运算- divide(double a, double b):除法运算CalculatorUI类:- createNumberButton(int number):创建数字按钮- createOperatorButton(String operator):创建运算符按钮- createDisplayScreen():创建显示屏Main类:- main(String[] args):主函数,启动计算器界面5. 实验结果分析通过实验,我们成功地实现了一个简单的计算器,用户能够输入数字并进行加、减、乘、除等运算。
java计算器实验报告
课程设计论文设计题目:java计算器学生姓名:指导教师:专业名称:所在院系:摘要在当今这个网络的时代,java语言在网络编程方面的优势使得网络编程有了更好的选择。
Java语言最大的特点是具有跨平台性,使其不受平台不同的影响,得到了广泛的应用。
该程序是一个图形界面的java 计算器,其界面主要采用了swing包,程序实现了:(1)实现简单加、减、乘、除的运算。
(2)实现除了加、减、乘、除按钮外其他按钮功能。
(3)给软件添加一个编辑、查看、帮助的主菜单。
(4)在数据输入方面,提高程序代码的健壮性,增强其对非法输入数据的识别能力。
(5)科学型计算器与标准计算器之间的菜单切换。
Java的早期版本以来,抽象窗口工具包为用户界面组件提供了平台独立的API。
在AWT中,每个组件都由一个原生的等同组件生成和控制,这个原生组件是由当前的图形窗口系统决定的。
与此相对,Swing 组件经常被描述为轻量级的,因为它们不需要操作系统本身所带窗口工具包的原生资源来生成。
目录第1章概要设计 (1)1.1查找资料 (1)1.2选题 (1)1.3标准型计算器界面设计 (1)1.4本章小节 (3)第2章程序整体设计说明 (4)2.1程序中所用到的变量 (4)2.2程序部分源代码及注释 (4)2.3本章小节 (15)第3章程序运行效果 (16)3.1程序运行主界面 (16)3.2程序中各按钮运行界面 (16)3.3本章小节 (20)第4章设计中遇到的重点及难点 (21)4.1设计中遇到的重点 (21)4.2设计中的遇到的难点 (21)4.3本章小节 (23)第5章本次设计中存在不足与改良方案 (24)5.1本次设计中存在的不足 (24)5.2本次设计的改良方案 (24)5.3本章小节 (24)结论 (25)参考文献 (26)致谢 (27)指导教师评语 (28)答辩委员会评语 (29)第1章概要设计1.1查找资料21世纪,随着社会经济的迅速发展和科学技术的全面进步,人类社会已进入信息和网络时代。
java课程设计报告计算器
目录一、课程设计目的 (2)二、课程设计任务 (2)2.1、设计任务 (2)2.2、课程设计要求: (2)2.3、需求分析 (2)三、开发工具与平台 (3)3.1、开发工具 (3)3.2、开发平台 (3)四、设计思路 (4)4.1、界面设计 (4)4.2.1、逻辑设计 (4)4.2.2、程序流程图 (5)4.2.3、主要代码展示及说明 (5)4.3、程序测试 (10)五、实验小结 (11)六、附录(程序代码) (12)一、课程设计目的1、熟练掌握java面向对象编程。
2、选择合适的数据结构实现需求。
3、熟练使用各种控制结构。
4、GUI组件、事件处理技术。
二、课程设计任务2.1、设计任务设计一个简易的计算器,可以进行四则运算:加、减、乘、除等(限于十进制下)程序要求:(1)应具有相应的界面,可参考Windows操作系统自带的计算器界面。
(2)操作符号定为:“+”,“-”,“*”,“/”,“+/-”等。
(按国际惯例设计)(3)用户通过点击程序界面上按钮,实现数字、运算符的输入操作。
(4)以上部分为必须完成的内容。
选作部分:(1)具有操作符号“1/x”,“sqrt”(开方),“.”(小数功能)等。
2.2、课程设计要求:(1)应用自己所学课程知识完成对计算器的基本任务。
(2)查阅相关资料,学习和掌握项目中涉及的新知识,提高自学能力。
(3)通过应用java程序编写计算器来提升自己对简单的图形界面有一定的掌握和了解。
2.3、需求分析1.设计的计算器可以完成加法、减法、乘法、除法的简单运算。
2.实现一些简单的扩展运算,如:正负号、倒数、退格、清零等功能。
3.添加小数点功能,用以实现浮点型数据的计算。
4.使用布局管理器设计一个计算器的界面,使用事件监听器处理数据的输入,并完成相关的计算。
三、开发工具与平台3.1、开发工具Microsoft Windows 7旗舰版3.2、开发平台JDK1.6.0-02 和UE编译器四、设计思路4.1、界面设计:(如图3-1)图3-14.2.1、逻辑设计:(1)根据所设计出来的界面,首先要设计其GUI界面,总体界面有一个文本框,20个按钮,总体界面用BorderLayout布局,文本框放置在最NORTH,然后0到9以及+,-,*,/等按钮放置到一个面板Panel中,完成界面设计。
【编程】第二期:加法计算器
【编程】第二期:加法计算器又到了一周一次的编程篇了,这一次我会教大家如何用程序做一个两个数之间的加法计算器。
和往常一样,打开Dev-c++ 用Ctrl+N新建一个项目,输入这几行基本上必打的代码。
接下来就是本篇文章的重点——定义变量。
在进行下一步之前我给大家列举一些常见的变量类型:int——整数型变量(只能存整数,不是整数时遵循省略小数部分的原则);float——单精度浮点型变量(可以储存7位有效数字);char——字符型变量(可以储存字符);double——双精度浮点数变量(可以储存15位有效数字);string——字符型变量(和char差不多,但是更好用);long——长整数型变量(和int用法一样,只不过可以储存的数字的范围扩大了);long long——超长整数型变量(比long可储存的范围还大……);虽然有这么多的变量类型,但其实需要记的变量类型不多。
你只需要记:int、double、char、string、long long就够了。
讲完了变量类型,接着就是如何定义。
其实定义变量的语句很简单,就是变量类型后面一个空格再加你要定义的变量的名称。
比如加法计算器中,我定义了两个变量,而这两个变量的名称我用了“x”和“y”来区分。
其实变量的名称可以由你来决定,只不过开头不能是数字罢了。
这就是加法计算器的完整的程序了。
其中的cin就是我上篇文章所提到的输入语句。
它和cout语句的不同就在于一个是用大于号连接,一个是小于号。
最后按F11编译运行,大家可以看到,这个计算器已经做好了。
当然,如果你想计算小数可以用我上文提到的double,这里我就不做了。
编程交流群二维码公众号二维码。
java实验报告——简单计算器的编写五篇范文
java实验报告——简单计算器的编写五篇范文第一篇:java实验报告——简单计算器的编写JAVA实验报告——简单计算器的编写班级:学号:姓名:一、实验目的1.掌握java图形用户界面(GUI)的设计原理和程序结构2.能设计复核问题要求的图形用户界面程序3.掌握常用组件的事件接口4.应用awt和swing组件进行应用程序设计二、实验条件1.计算机一台2.java软件开发环境三、实验步骤1、编写代码:mport java.awt.*;import java.awt.event.*;import javax.swing.*;public class JCalculator extends JFrame implements ActionListener {private static final long serialVersionUID =-***457Lprivate class WindowCloser extends WindowAdapter {public void windowClosing(WindowEvent we){System.exit(0);}}int i;private final String[] str = { “7”, “8”, “9”, “/”, “4”, “5”, “6”, “*”, “1”,“2”, “3”, “-”, “.”, “0”, “=”, “+” };JButton[] buttons = new JButton[str.length]; JButton reset = new JButton(“CE”); JTextField display = new JTextField(“0”);public JCalculator(){super(“Calculator”);JPanel panel1 = new JPanel(new GridLayout(4, 4)); for(i = 0;i < str.length;i++){buttons[i] = new JButton(str[i]);panel1.add(buttons[i]);}JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(“Center”, display);panel2.add(“East”, reset);getContentPane().setLayout(new BorderLayout()); getContentPane().add(“North”, panel2); getContentPane().add(“Center”, panel1);for(i = 0;i < str.length;i++)buttons[i].addActionListener(this);reset.addActionListener(this);display.addActionListener(this); addWindowListener(new WindowCloser()); setSize(800, 800);setVisible(true);pack();}public void actionPerformed(ActionEvent e){ Object target = e.getSource();String label = e.getActionCommand();if(target == reset)handleReset();else if(“0123456789.”.indexOf(label)> 0)handleNumber(label);elsehandleOperator(label);}boolean isFirstDigit = true;public void handleNumber(String key){if(isFirstDigit)display.setText(key);else if((key.equals(“.”))&&(display.getText().indexOf(“.”)< 0))display.setText(display.getText()+ “.”);else if(!key.equals(“.”))display.setText(display.getText()+ key);isFirstDigit = false;}public void handleReset(){display.setText(“0”);isFirstDigit = true;operator = “=”;}double number = 0.0;String operator = “=”;public void handleOperator(String key){if(operator.equals(“+”))number += Double.valueOf(display.getText());else if(operator.equals(“-”))number-= Double.valueOf(display.getText());else if(operator.equals(“*”))number *= Double.valueOf(display.getText());else if(operator.equals(“/”))number /= Double.valueOf(display.getText());else if(operator.equals(“=”))number = Double.valueOf(display.getText());display.setText(String.valueOf(number));operator = key;isFirstDigit = true;}public static void main(String[] args){new JCalculator();} }2、运行结果,见截图计算测试:123+456=579结果正确,程序无误。
Java程序设计计算器实验报告
长江大学工程技术学院实验报告
通信 61302 胡炯
} else if (operator.equals("-")) { // 减法运算 resultNum -= getNumberFromText(); } else if (operator.equals("*")) { // 乘法运算 resultNum *= getNumberFromText(); } else if (operator.equals("sqrt")) { // 平方根运算 resultNum = Math.sqrt(resultNum); } else if (operator.equals("%")) { // 百分号运算,除以 100 resultNum = resultNum / 100; } else if (operator.equals("+/-")) { // 正数负数运算 resultNum = resultNum * (-1); } else if (operator.equals("=")) { // 赋值运算 resultNum = getNumberFromText(); } if (operateValidFlag) { // 双精度浮点数的运算 long t1; double t2; t1 = (long) resultNum; t2 = resultNum - t1; if (t2 == 0) { resultText.setText(String.valueOf(t1)); } else { resultText.setText(String.valueOf(resultNum)); } } (2)主要功能过程说明 1 构造函数 2 初始化计算器 3 处理事件 4 处理 Backspace 键被按下的事件 5 处理数字键被按下的事件 6 处理 C 键被按下的事件 7 处理运算符键被按下的事件 8 从结果文本框中获取数字 (3)功能流程图
基于java的简易计算器的设计
基于java的简易计算器的设计摘要自从java语言诞生以来,java语言就以不可抵挡的趋势很快成为国际上广泛流行的面向对象编程语言,它既具有高级语言的特点,又少了C语言中指针特点,因而变得简单了很多。
Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,其具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。
本文介绍了用java语言编写一个简易计算器。
实现数学上的加、减、乘、除、乘方、开方、阶乘、正弦、余弦运算等相关运算。
利用这个程序可以方便的进行日常性的小规模运算,小巧简易,是个实用的工具。
关键词:程序设计;简易计算器;java语言THE DESIGN OF SIMPLE CALCULATOR BASED ON JA V AABSTRACTSince the inception of the java language, java language Take irresistible trend soon beca me widespread international popular object-oriented programming language, both with advan ced language features, and less of the C language pointer characteristics, and thus becomes a l ot simpler . Java is a cross-platform application software can write object-oriented programmi ng language, which has excellent versatility, efficiency, platform portability, and security, are widely used in personal PC, data center, gaming consoles, scientific super-computers, mobile phones and the Internet, also has the world's largest developer of professional community. In t he global cloud computing and mobile Internet industry environment, Java and more have sig nificant advantages and broad prospects.This article describes the use java language a simple calculator. Achieve mathematical addition, subtraction, multiplication, division, involution, evolution, factorial, sine, cosine op erations and other related operations. With this program you can easily carry out daily operati ons of small-scale, small simple, is a useful tool.Key words:program design;simple calculator;java language目录1前言 (1)1.1 课程设计背景 (1)1.2 需求环境 (1)1.3 课程设计思路 (1)2课程设计概要 (2)3 计算器详细设计 (3)3.1 计算器界面……….………….........………………………………………. .33.1.1 CAL主类的显示分布 (3)3.1.2计算器显示界面实现代码 (3)3.2 监听各个按钮的类 (5)3.2.1 编写监听类目的 (5)3.2.2 监听类实现代码 (5)3.3 按键响应类 (7)3.3.1 响应类编写的目的 (7)3.2.2 响应类实现代码 (7)3.3 运算处理类 (9)3.3.1 处理方式 . (9)3.2.2 处理类实现代码 (9)4 运行与调试 (12)4.1 进入程序主界面 (12)4.2 加减乘除功能实现 (12)4.3 正余弦、正切功能实现 (13)4.4 阶乘功能实现 (13)4.5 乘方、开方功能实现 (14)5 实验总结 (15)参考文献 (16)附录:源代码 (17)1 前言1.1 课程设计背景日常生活中我们经常会遇到一些小型数据计算的问题,本课程设计就是以此为出发点,设计了这样一个简单的计算器,用以实现基本的数学运算。
eclipse实现简单计算器代码.
eclipse实现简单计算器代码.package Computer;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.util.LinkedList;import java.text.NumberFormat;public class Cacultor extends Frame implements ActionListener{/*** @param args*/NumberButton numberButton[];OperationButton operationButton[];Button 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮;Panel panel;JTextField resultShow;String 运算符号[]={"+","-","*","/"};LinkedList 链表;boolean 是否按下等号=false;public Cacultor(){super("计算器");链表=new LinkedList();numberButton=new NumberButton[10];for(int i=0;i<=9;i++){numberButton[i]=new NumberButton(i);numberButton[i].addActionListener(this);}operationButton=new OperationButton[4];for(int i=0;i<4;i++){operationButton[i]=new OperationButton(运算符号[i]);operationButton[i].addActionListener(this);}小数点按钮=new Button(".");正负号按钮=new Button("+/-");等号按钮=new Button("=");求倒数按钮=new Button("1/x");退格按钮=new Button("退格");清零按钮=new Button("c");清零按钮.setForeground(Color.red);退格按钮.setForeground(Color.red);等号按钮.setForeground(Color.red);求倒数按钮.setForeground(Color.blue);正负号按钮.setForeground(Color.blue);小数点按钮.setForeground(Color.blue);退格按钮.addActionListener(this);清零按钮.addActionListener(this);等号按钮.addActionListener(this);小数点按钮.addActionListener(this);正负号按钮.addActionListener(this);求倒数按钮.addActionListener(this);resultShow=new JTextField(10);resultShow.setHorizontalAlignment(JTextField.RIGHT); resultShow.setForeground(Color.blue);resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14)); resultShow.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));resultShow.setBackground(Color.white);resultShow.setEditable(false);panel=new Panel();panel.setLayout(new GridLayout(4,5));panel.add(numberButton[1]);panel.add(numberButton[2]);panel.add(numberButton[3]);panel.add(operationButton[0]);panel.add(清零按钮);panel.add(numberButton[4]);panel.add(numberButton[5]);panel.add(numberButton[6]);panel.add(operationButton[1]);panel.add(退格按钮);panel.add(numberButton[7]);panel.add(numberButton[8]);panel.add(numberButton[9]);panel.add(operationButton[2]);panel.add(求倒数按钮);panel.add(numberButton[0]);panel.add(正负号按钮);panel.add(小数点按钮);panel.add(operationButton[3]);panel.add(等号按钮);add(panel,BorderLayout.CENTER);add(resultShow,BorderLayout.NORTH);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){ System.exit(0);}});setVisible(true);setBounds(100,50,240,180);setResizable(false);validate();}//按钮事件的处理public void actionPerformed(ActionEvent e){if(e.getSource() instanceof NumberButton){NumberButton b=(NumberButton)e.getSource();if(链表.size()==0){int number=b.getNumber();链表.add(""+number);resultShow.setText(""+number);是否按下等号=false;}else if(链表.size()==1&&是否按下等号==false){ int number=b.getNumber();String num=(String)链表.getFirst();String s=num.concat(""+number);链表.set(0, s);resultShow.setText(s);}else if(链表.size()==1&&是否按下等号==true){int number=b.getNumber();链表.removeFirst();链表.add(""+number);是否按下等号=false;resultShow.setText(""+number);}else if(链表.size()==2){int number=b.getNumber();链表.add(""+number);resultShow.setText(""+number);}else if(链表.size()==3){int number=b.getNumber();String num=(String)链表.getLast();String s=num.concat(""+number);链表.set(2, s);resultShow.setText(s);}}else if(e.getSource()instanceof OperationButton){ OperationButton b=(OperationButton)e.getSource();if(链表.size()==1){String fuhao=b.get运算符号();链表.add(fuhao);}else if(链表.size()==2){String fuhao=b.get运算符号();链表.set(1, fuhao);}else if(链表.size()==3){String fuhao=b.get运算符号();String number1=(String)链表.getFirst();String number2=(String)链表.getLast();String 运算符号=(String)链表.get(1);try{double n2=Double.parseDouble(number2); double n=0;if(运算符号.equals("+")){n=n1+n2;}else if(运算符号.equals("-")){n=n1-n2;}else if(运算符号.equals("*")){n=n1*n2;}else if(运算符号.equals("/")){n=n1/n2;}链表.clear();链表.add(""+n);链表.add(fuhao);resultShow.setText(""+n);}catch(Exception ee){}}}else if(e.getSource()==等号按钮){是否按下等号=true;if(链表.size()==1&&链表.size()==2){ String num=(String)链表.getFirst(); resultShow.setText(""+num);}else if(链表.size()==3){String number1=(String)链表.getFirst(); String number2=(String)链表.getLast(); String 运算符号=(String)链表.get(1);try{double n2=Double.parseDouble(number2); double n=0;if(运算符号.equals("+")){n=n1+n2;}else if(运算符号.equals("-")){n=n1-n2;}else if(运算符号.equals("*")){n=n1*n2;}else if(运算符号.equals("/")){n=n1/n2;}resultShow.setText(""+n);链表.set(0, ""+n);链表.removeLast();链表.removeLast();}catch(Exception ee){}}}else if(e.getSource()==小数点按钮){if(链表.size()==0){是否按下等号=false;}else if(链表.size()==1){String dot=小数点按钮.getLabel();String num=(String)链表.getFirst();String s=null;if(num.indexOf(dot)==-1){s=num.concat(dot);链表.set(0, s);}else{s=num;}链表.set(0, s);resultShow.setText(s);}else if(链表.size()==3){String dot=小数点按钮.getLabel(); String num=(String)链表.getLast(); String s=null;if(num.indexOf(dot)==-1){s=num.concat(dot);链表.set(2, s);}else{s=num;}resultShow.setText(s);}}else if(e.getSource()==退格按钮){if(链表.size()==1){String num=(String)链表.getFirst();if(num.length()>=1){num=num.substring(0,num.length()-1); 链表.set(0, num);resultShow.setText(num);}else{链表.removeLast();resultShow.setText("0");}}else if(链表.size()==3){String num=(String)链表.getLast();if(num.length()>=1){num=num.substring(0,num.length()-1); 链表.set(2, num);resultShow.setText(num);}else{链表.removeLast();resultShow.setText("0");}}}else if(e.getSource()==正负号按钮){if(链表.size()==1){String number1=(String)链表.getFirst(); try{double d=Double.parseDouble(number1); d=-1*d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);}catch(Exception ee){}}else if(链表.size()==3){String number2=(String)链表.getLast();try{double d=Double.parseDouble(number2); d=-1*d;String str=String.valueOf(d);链表.set(2, str);resultShow.setText(str);}catch(Exception ee){}}}else if(e.getSource()==求倒数按钮){if(链表.size()==1||链表.size()==2){String number1=(String)链表.getFirst(); try{double d=Double.parseDouble(number1); d=1.0/d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);}catch(Exception ee){}}else if(链表.size()==3){String number2=(String)链表.getLast();try{double d=Double.parseDouble(number2); d=1.0/d;String str=String.valueOf(d);链表.set(0, str);resultShow.setText(str);}catch(Exception ee){}}}else if(e.getSource()==清零按钮){是否按下等号=false;resultShow.setText("0");链表.clear();}}public static void main(String[] args) {// TODO Auto-generated method stub new Cacultor();}}。
计算器设计,包括数字按钮0-9,以及加减乘除法
使用Java Application编写满足下列要求的程序:(1)使用GridLayout布局(2)设计一个简单的计算器,包括数字按钮0-9,以及加、减、乘、除、清零,等于运算按钮和一个现实计算结果的文本区。
import java.applet.*;import java.awt.*;import java.awt.event.*;public class TestText extends Applet implements ActionListener {Label lb1,lb2;//Button btn1;Button btn2;TextField tf1;TextArea ta1;public void init(){lb1=new Label("请在文本框中输入内容!");lb2=new Label("文本区显示的内容为:");//btn1=new Button("提交");btn2=new Button("退出");tf1=new TextField(20);ta1=new TextArea();add(lb1);add(tf1);add(lb2);add(ta1);//add(btn1);add(btn2);setLayout(new FlowLayout());setSize(600,400);setVisible(true);//btn1.addActionListener(this);btn2.addActionListener(this);}public void actionPerformed(ActionEvent e){/*if(e.getSource()==btn1){ta1.setText(tf1.getText());}*/if(e.getSource()==btn2){System.exit(0);}}}(8)import java.applet.*;import java.awt.*;import java.awt.event.*;public class JiSuan extends Applet implements ActionListener{Button []buts;String names[] = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","置零"};TextArea text;public void init(){text = new TextArea(" ",20,2);add(text);setLayout(new GridLayout(9,4,5,5));buts = new Button[names.length];for(int i = 0;i < names.length;i++){buts[i] = new Button(names[i]);buts[i].addActionListener(this);add(buts[i]);}}static int y = 0;static float j = 0;static int z = 0;public void actionPerformed(ActionEvent e){for(int i = 0;i < 10;i++)if(e.getSource() == buts[i]){char x;x = (char)(i + '0');text.append(""+x);y = y * 10 + i;}if(e.getSource() == buts[10]) {int i = y;y = 0;j = j + i;z = 1;text.append(" + ");}if(e.getSource() == buts[11]) {int i = y;y = 0;if(j == 0)j = j + i;elsej = j - i;z = 2;text.append(" - ");}if(e.getSource() == buts[12]) {int i = y;y = 0;if(j == 0)j = j + i;elsej = j * i;z = 3;text.append(" * ");}if(e.getSource() == buts[13]) {int i = y;y = 0;if(j == 0)j = j + i;elsej = j / i;z = 4;text.append(" / ");}if(e.getSource() == buts[14]) {if(z == 1)j = j + y;else if(z == 2)j = j - y;else if(z == 3)j = j * y;elsej = j / y;text.append(" = "+j);y = 0;}if(e.getSource() == buts[15]) {text.replaceRange(" ",0,50); y = 0;j = 0;}repaint();}}。
JAVA RMI 对计算器编程实现
JAVA RMI 对计算器编程实现摘要:本文围绕Java编程语言在网络编程方面的具体应用,论述了面向对象方法,对计算器程序进行需求分析、概要设计、详细设计,最后使用java rmi编程实现了全过程。
Java 作为一种风靡一时的网络开发语言,其巨大的威力就体现在它强大的开发分布式网络应用的能力上,而RMI就是开发百分之百纯Java的网络分布式应用系统的核心解决方案之一。
其实它可以被看作是RPC的Java版本。
但是传统RPC并不能很好地应用于分布式对象系统。
而Java RMI 则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。
RMI目前使用Java远程消息交换协议JRMP(Java RemoteMessaging PRotocol)进行通信。
JRMP是专为Java的远程对象制定的协议。
因此,Java RMI 具有Java的"Write Once,Run Anywhere"的优点,是分布式应用系统的百分之百纯Java解决方案。
用Java RMI开发的应用系统可以部署在任何支持JRE(Java Run Environment Java,运行环境)的平台上。
本文拟从程序的角度举例介绍怎样利用RMI实现Java分布式应用。
关键词:JAVA RMI 分布式应用Abstract : This paper focuses on the specific application in the Java programming language network programming , discusses the object-oriented method, the calculator program needs analysis , outline design , detailed design , and finally the use of java rmi programming the entire process. As a rage Java web development language, and its enormous power is reflected in the ability of its powerful development of distributed network applications , while the core 100% pure Java RMI is to develop a network of distributed application system one of the solutions . In fact, it can be seen as RPC Java version. But traditional RPC is not well applied to distributed object systems . The Java RMI supports communication between the stored procedures in different address spaces each object class , seamless remote call remote objects. Currently the use of Java RMI remote message exchange protocol JRMP (Java Remote Messaging PRotocol) communication. JRMP is a protocol designed for Java remote object making. Thus , the advantages of "Write Once, Run Anywhere" The Java RMI with Java , and is 100% pure Java distributed application system solutions. Applications developed using Java RMI systems can be deployed on any platform supported JRE (Java Run Environment Java, Runtime Environment) . This paper , for example from the perspective of how to introduce the use of the program 's implementation of Java RMI distributed applications .Keywords: JAVA RMI Distributed Application1引言随着科学技术的发展,计算机领域也在飞速的发展,人们对于计算器的要求越来越高。
Java语言程序设计实训【计算器】
目录一、课程设计的目的与要求 (2)1.1 课程设计目的 (2)1.2 课程设计要求 (2)二、题目说明 (2)2.1 题目说明 (2)2.2 开发工具介绍 (2)三、总体设计 (3)3.1 系统开发平台 (3)3.2 系统结构图 (3)四、详细说明 (3)4.1 计算器主界面 (3)4.2 系统实施 (4)五、遇到的问题和解决方法 (9)六、总结 (10)七、参考文献 (10)附录(源程序代码) (11)八、教师评语 (20)一、课程设计的目的与要求1.1 课程设计目的1) 复习,巩固Java语言的基础知识,进一步加深对Java语言的理解和掌握。
2) 课程设计为学生提供了一个既动手又动脑,独立实践的机会,将课本上的理论知识和实际有机的结合起来,锻炼学生的分析解决实际问题的能力,提高学生适应实际,实践编程的能力。
3) 培养学生在项目开发中团队合作精神,创新意识及能力。
1.2 课程设计要求◆功能要求:该程序显示GUI用户界面,至少能实现整数的加,减,乘,除四则运算.◆界面要示:用图形界面实现◆实训条件:计算机房◆硬件要求:装有Winxp等操作系统的机器◆软件要求: JDK、JCreator等开发工具二、题目说明2.1 题目说明随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺少的工具。
文本编辑器是个人计算机最司空见惯的应用程序了,在学习了Java语言之后,我决定使用Java语言编写一个简单的计算器,可以实现简单的运算功能,满足日常基本的工作学习和娱乐需要。
该程序是一个图形界面的简单的java计算器,使用人员能快捷简单地进行操作。
即时准确地获得需要的计算的结果,充分降低了数字计算的难度和节约了时间。
可以进行简单的四则运算(加、减、乘、除,以及求倒数,求相反数),以及常用的数学函数(sin,cos,log,1/x,pow,sqrt,%,)和记忆处理功能,有退格,清零, 归零等应用。
界面颜色为白色,数字字体颜色为蓝色,函数及其他功能键为红色。
java编写的计算器设计报告
java编写的计算器设计报告实验7:综合实验二一、试验目的进一步掌握图形用户界面GUI,了解Swing组件的使用,以及系统提供的该工具的作用,进一步掌握JAVA事件响应机制的原理,更好的掌握面向对象编程的思路。
二、实验要求创建一个界面来实现一个简单的计算器,要求:1、实现最基本的计算器界面,包括:0~9的10个数字按钮,加、减、乘、除、等于5个运算符按钮,一个结果存放的文本区域。
2、实现最基本的加、减、乘、除运算,并能得到正确结果。
3、实现连续的运算,小数点的使用,并考虑各种可能导致异常的情况,将程序作完善;4、可以通过关闭按钮实现关闭窗口。
三、实验步骤、结果1、程序代码:import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent; importjava.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;public class ZhxCacu extends JFrame implements ActionListener { JPanel jpResult = new JPanel();JPanel jpButton = new JPanel();JTextField jtfResult = new JTextField("0");JButton zero = new JButton("0"); //数字键0JButton one = new JButton("1"); //数字键1JButton two = new JButton("2"); //数字键2JButton three = new JButton("3"); //数字键3JButton four = new JButton("4"); //数字键4JButton five = new JButton("5"); //数字键5JButton six = new JButton("6"); //数字键6JButton seven = new JButton("7"); //数字键7JButton eight = new JButton("8"); //数字键8JButton nine = new JButton("9"); // 数字键9JButton plus = new JButton("+");JButton sub = new JButton("-");JButton mul = new JButton("*");JButton div = new JButton("/");JButton equal = new JButton("=");JButton ce = new JButton("ce"); // 置零键JButton point = new JButton(".");JButton tzero = new JButton("00");//com代表敲击运算符,digit代表敲击数字键boolean com = false;boolean digit = false;float total=0;String sum="";int symbol=0;int b,c=0;public ZhxCacu(){// 添加结果输入框并设置对齐方式jpResult.setLayout(new BorderLayout());jpResult.add(jtfResult);jtfResult.setEditable(false);jtfResult.setHorizontalAlignment(SwingConstants.RIGHT); //将组件添加到窗体上this.add(jpResult,"North");this.add(jpButton,"Center");// 定义按钮区域布局管理器为网格布局jpButton.setLayout(new GridLayout(6, 3, 10, 10));// 添加各个按钮键jpButton.add(seven);jpButton.add(eight);jpButton.add(nine);jpButton.add(four);jpButton.add(five);jpButton.add(six);jpButton.add(one);jpButton.add(two);jpButton.add(three);jpButton.add(zero);jpButton.add(tzero);jpButton.add(plus);jpButton.add(sub);jpButton.add(mul);jpButton.add(div);jpButton.add(point);jpButton.add(equal);jpButton.add(ce);one.addActionListener(this);//对1按钮添加监听事件two.addActionListener(this);//对2按钮添加监听事件three.addActionListener(this);//对3按钮添加监听事件four.addActionListener(this);//对4按钮添加监听事件five.addActionListener(this);//对5按钮添加监听事件six.addActionListener(this);//对6按钮添加监听事件seven.addActionListener(this);//对7按钮添加监听事件eight.addActionListener(this);//对8按钮添加监听事件nine.addActionListener(this);//对9按钮添加监听事件zero.addActionListener(this);//对0按钮添加监听事件ce.addActionListener(this);//对置零按钮添加监听事件plus.addActionListener(this);//对+按钮添加监听事件equal.addActionListener(this);//对=按钮添加监听事件sub.addActionListener(this);//对-按钮添加监听事件mul.addActionListener(this);//对*按钮添加监听事件div.addActionListener(this);//对/按钮添加监听事件tzero.addActionListener(this);//对00按钮添加监听事件point.addActionListener(this);//对.按钮添加监听事件pack();//初始化窗体大小最合适大小this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});this.setSize(300,300);this.setVisible(true);}public static void main(String[] args) {// TODO 自动生成方法存根new ZhxCacu();}public void actionPerformed(ActionEvent e) {// TODO 自动生成方法存根//数字if(e.getSource()==one){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("1");com = false;digit = true;}else{sum = jtfResult.getText()+"1"; jtfResult.setText(sum);}}else if(e.getSource()==two){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("2");com = false;digit = true;}else{sum = jtfResult.getText()+"2"; jtfResult.setText(sum);}}else if(e.getSource()==two){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("2");com = false;digit = true;}else{sum = jtfResult.getText()+"2"; jtfResult.setText(sum);}}else if(e.getSource()==two){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("2");com = false;digit = true;}else{sum = jtfResult.getText()+"2"; jtfResult.setText(sum);}}else if(e.getSource()==two){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("2");com = false;digit = true;}else{sum = jtfResult.getText()+"2"; jtfResult.setText(sum);}}else if(e.getSource()==three){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("3");com = false;digit = true;}else{sum = jtfResult.getText()+"3"; jtfResult.setText(sum);}}else if(e.getSource()==four){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("4");com = false;digit = true;}else{sum = jtfResult.getText()+"4"; jtfResult.setText(sum);}}else if(e.getSource()==five){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("5");com = false;digit = true;}else{sum = jtfResult.getText()+"5"; jtfResult.setText(sum);}}else if(e.getSource()==six){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("6");com = false;digit = true;}else{sum = jtfResult.getText()+"6"; jtfResult.setText(sum);}}else if(e.getSource()==seven){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("7");com = false;digit = true;}else{sum = jtfResult.getText()+"7";jtfResult.setText(sum);}}else if(e.getSource()==eight){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("8");com = false;digit = true;}else{sum = jtfResult.getText()+"8"; jtfResult.setText(sum);}}else if(e.getSource()==nine){if(com||digit==false){//第一次敲击数字按钮jtfResult.setText("9");com = false;digit = true;}else{sum = jtfResult.getText()+"9"; jtfResult.setText(sum);}}else if(e.getSource()==zero){if(com||digit==false){//第一次敲击数字按jtfResult.setText("0");com = false;digit = true;}else{sum = jtfResult.getText()+"0"; jtfResult.setText(sum);if(Float.parseFloat(sum)!=0){}else{if(b==1){sum = jtfResult.getText()+"0"; jtfResult.setText(sum);}elsejtfResult.setText("0");}}}else if(e.getSource()==tzero){if(com||digit==false){//第一次敲击数字按jtfResult.setText("00");com = false;digit = true;}else{sum = jtfResult.getText()+"00";jtfResult.setText(sum);}}else if(e.getSource()==point){if(com||digit==false){//第一次敲击数字按钮b=1;com = true;digit = false;}else if(c==1); else{b=1;sum = jtfResult.getText()+".";jtfResult.setText(sum);c=1;}}//运算else if(e.getSource()==plus){symbol = 1;c=0;total = Float.parseFloat(jtfResult.getText()); com = true;digit = false;}else if(e.getSource()==sub){symbol = 2;c=0;total = Float.parseFloat(jtfResult.getText()); com = true;digit = false;}else if(e.getSource()==mul){symbol = 3;c=0;total = Float.parseFloat(jtfResult.getText()); com = true;digit = false;}else if(e.getSource()==div){symbol = 4;c=0;total = Float.parseFloat(jtfResult.getText()); com = true;digit = false;}else if(e.getSource()==ce){com = true;digit = false;total=0;sum ="0" ;jtfResult.setText(sum);}//=else if(e.getSource()==equal){com = true;digit = false;switch(symbol){case 1:jtfResult.setText(newFloat(total+Float.parseFloat(jtfResult.getText())).toString());b=0;c=0;b reak;case 2:jtfResult.setText(newFloat(total-Float.parseFloat(jtfResult.getText())).toString());b=0;c=0;break;case 3:jtfResult.setText(newFloat(total*Float.parseFloat(jtfResult.getText())).toString());b=0;c =0;break;case 4:if( Float.parseFloat(jtfResult.getText())==0 ){try{throw new Exception();}catch(Exception a){jtfResult.setText("错误~被除数不能为0,请重新输入:");}}elsejtfResult.setText(newFloat(total/Float.parseFloat(jtfResult.getText())).toString());b=0;c =0;break;}digit=false;total = 0;}}}2、界面:四、实验中的问题以及解决方案:1、问题: 被除数为0解决:抛出异常2、问题: 阻止0、小数点在同一数字中重复出现解决: 设置标志,五、总结:1、进一步了解了项目开发的步骤,思路,以及程序的布局和框架结构,尤其是对JAVA的模块化设计有了更为深入的了解。
Java实现计算器
Java实现简易计算器,可以计算算术表达式:Java源码如下://GridFrame.java文件import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;//计算机框架布局public class GridFrame extends JFrame{//定义面板,并设置为网格布局,4行4列,组件水平、垂直间距均为3JPanel p=new JPanel(new GridLayout(5,4,3,3));JTextArea out=new JTextArea();//定义文本框String temp="";//定义字符串数组,为按钮的显示文本赋值//注意字符元素的顺序与循环添加按钮保持一致Stringstr[]={"C","(",")","BS","7","8","9","/","4","5","6"," *","1","2","3","-","0",".","=","+"};JButton btn[]=new JButton[str.length]; //声明按钮数组public GridFrame(String s){super(s);//为窗体名称赋值setLayout(new BorderLayout());//定义窗体布局为边界布局//循环定义按钮,并添加到面板中for(int i=0;i<str.length;i++){btn[i]=new JButton(str[i]);btn[i].addActionListener(al);p.add(btn[i]);}//将文本框放置在窗体NORTH位置getContentPane().add(out,BorderLayout.NORTH);out.setRows(2);//将面板放置在窗体CENTER位置getContentPane().add(p,BorderLayout.CENTER);setVisible(true);setSize(350,250);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);//让窗体居中显示}//实现监听器ActionListener al=new ActionListener(){public void actionPerformed(ActionEvent e){Object currKey=e.getSource();if(currKey==btn[12]){out.setText(out.getText()+"1");}else if(currKey==btn[13]){out.setText(out.getText()+"2");}else if(currKey==btn[14]){out.setText(out.getText()+"3");}else if(currKey==btn[8]){}else if(currKey==btn[9]){ out.setText(out.getText()+"5"); }else if(currKey==btn[10]){ out.setText(out.getText()+"6"); }else if(currKey==btn[4]){ out.setText(out.getText()+"7"); }else if(currKey==btn[5]){ out.setText(out.getText()+"8"); }else if(currKey==btn[6]){ out.setText(out.getText()+"9"); }else if(currKey==btn[16]){ out.setText(out.getText()+"0"); }else if(currKey==btn[17]){ out.setText(out.getText()+"."); }else if(currKey==btn[1]){ out.setText(out.getText()+"("); }else if(currKey==btn[2]){ out.setText(out.getText()+")"); }else if(currKey==btn[7]){ out.setText(out.getText()+"/"); }else if(currKey==btn[11]){}else if(currKey==btn[15]){out.setText(out.getText()+"-");}else if(currKey==btn[19]){out.setText(out.getText()+"+");}else if(currKey==btn[18]){out.setText(out.getText()+"\n=");temp=out.getText();temp=temp.substring(0,out.getText().length()-2);;Calculator cal=new Calculator(temp);out.setText(out.getText()+String.valueOf(cal.getRes ult()));}//清除键if(currKey==btn[0]){out.setText("");}//退格键if(currKey==btn[3]){String str=out.getText();if(str.length()!=0){String str1=str.substring(0, str.length()-1);out.setText(str1);}}}};public static void main(String[]args){GridFrame gl=new GridFrame("简易计算机!");}}//ArithHelper.java文件public class ArithHelper{//默认除法运算精度private static final int DEF_DIV_SCALE=16;//这个类不能实例化private ArithHelper(){}/***提供精确的加法运算。
JAVA计算器源代码
计算器源代码一、计算器源代码文件名:computer1.javaimport java.awt.*;import java.awt.event.*;public class computer1 extends Frame implements ActionListener{//声明窗口类并实现动作事件接口。
Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮Button op,os,om,od,oe,oc;//声明操作按钮TextField tfd;//声明文本框String flg,rslt;//声明标志串、结果串Panel p1,p2,p3;//声明面板int i1,i2;float flt;computer1(){super("加减乘除计算器");n0 = new Button("0");//实现各按钮n1 = new Button("1");n2 = new Button("2");n3 = new Button("3");n4 = new Button("4");n5 = new Button("5");n6 = new Button("6");n7 = new Button("7");n8 = new Button("8");n9 = new Button("9");op = new Button("加");os = new Button("减");om = new Button("乘");od = new Button("除");oe = new Button("=");oc = new Button("c");tfd = new TextField(20);//实现文本框p1=new Panel();//实现各面板p2=new Panel();p3=new Panel();setLayout(new FlowLayout());//布局设计,用于安排按钮位置p1.add(n0);//将各数字按钮放入p1中p1.add(n1);p1.add(n2);p1.add(n3);p1.add(n4);p1.add(n5);p1.add(n6);p1.add(n7);p1.add(n8);p1.add(n9);p2.add(op);//将各操作按钮放入p2、p3中p2.add(os);p2.add(om);p2.add(od);p3.add(oe);p3.add(oc);setLayout(new BorderLayout());//布局设计,用于安排面板位置add("North",tfd);add("West",p1);add("Center",p2);add("East",p3);n0.addActionListener(this);//注册监听器到各按钮n1.addActionListener(this);n2.addActionListener(this);n3.addActionListener(this);n4.addActionListener(this);n5.addActionListener(this);n6.addActionListener(this);n7.addActionListener(this);n8.addActionListener(this);n9.addActionListener(this);op.addActionListener(this);os.addActionListener(this);om.addActionListener(this);od.addActionListener(this);oe.addActionListener(this);oc.addActionListener(this);addWindowListener(new closeWin());setSize(600,100);//确定窗口的尺寸setVisible(true);}public static void main (String args[]){new computer1();}public void actionPerformed(ActionEvent e){//处理鼠标事件的方法try{//异常处理if(e.getSource()==n0)//按数字键时tfd.setText(tfd.getText()+"0");if(e.getSource()==n1)tfd.setText(tfd.getText()+"1");if(e.getSource()==n2)tfd.setText(tfd.getText()+"2");if(e.getSource()==n3)tfd.setText(tfd.getText()+"3");if(e.getSource()==n4)tfd.setText(tfd.getText()+"4");if(e.getSource()==n5)tfd.setText(tfd.getText()+"5");if(e.getSource()==n6)tfd.setText(tfd.getText()+"6");if(e.getSource()==n7)tfd.setText(tfd.getText()+"7");if(e.getSource()==n8)tfd.setText(tfd.getText()+"8");if(e.getSource()==n9)tfd.setText(tfd.getText()+"9");if(e.getSource()==op){//按加号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "op";}if(e.getSource()==os){//按减号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "os";}if(e.getSource()==om){//按乘号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "om";}if(e.getSource()==od){//按除号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "od";}if(e.getSource()==oe){//按等号键时i2 = Integer.parseInt(tfd.getText());if(flg=="op"){rslt=Integer.toString(i1+i2);}if(flg=="os"){rslt=Integer.toString(i1-i2);}if(flg=="om"){rslt=Integer.toString(i1*i2);}if(flg=="od"){//除法需做小数处理flt=((float)i1)/((float)i2);rslt=Float.toString(flt);}tfd.setText(rslt);}if(e.getSource()==oc){//按清除键时tfd.setText("");flg = "";}}catch(Exception ex){}//扑捉到异常,但不进行处理}}class closeWin extends WindowAdapter{ //关闭窗口public void windowClosing(WindowEvent e){Frame frm=(Frame)(e.getSource());frm.dispose();System.exit(0);}}二、计算器界面三、修改后计算器界面。
java简易计算器完整代码
java简易计算器完整代码import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculate extends JFrame implements ActionListener {/****/private static final long serialVersionUID = 1L;/*** @param args*/float userFloata=0f;float userFloatb=0f;double result;JLabel label1 ;JLabel label2;JLabel label3;JTextField textField1;JTextField textField2;JTextField textField3;JButton addButton;JButton subtractButton;JButton rideButton;JButton divideButton;public Calculate(){setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("简易计算器");JPanel viewJPanel_1=new JPanel();JPanel viewJPanel_2=new JPanel();addButton=new JButton("加");addButton.addActionListener(this);viewJPanel_1.add(addButton);subtractButton=new JButton("减");subtractButton.addActionListener(this);viewJPanel_1.add(subtractButton);rideButton=new JButton("乘");rideButton.addActionListener(this);viewJPanel_1.add(rideButton);divideButton=new JButton("除");divideButton.addActionListener(this);viewJPanel_1.add(divideButton);label1=new JLabel("数据1:");viewJPanel_2.add(label1);textField1=new JTextField(5);textField1.addActionListener(this);viewJPanel_2.add(textField1);label2=new JLabel("数据2:");viewJPanel_2.add(label2);textField2=new JTextField(5);textField2.addActionListener(this);viewJPanel_2.add(textField2);label3=new JLabel("结果是:");viewJPanel_2.add(label3 );textField3=new JTextField(5);viewJPanel_2.add(textField3);setSize(420,120);setVisible(true);validate();getContentPane().add(viewJPanel_1,BorderLayout.NORTH);getContentPane().add(viewJPanel_2,BorderLayout.CENTER);}public void CalculateEventHandel(){try{userFloata= Float.parseFloat(textField1.getText().trim());userFloatb= Float.parseFloat(textField2.getText().trim());} catch (NumberFormatException e) {JOptionPane.showMessageDialog(this, "请输入数字型数据!");textField1.setText("");textField1.requestFocus();textField2.setText("");textField3.setText("");return;}}public static void main(String[] args) { // TODO Auto-generated method stubnew Calculate();//a.CalculateEventHandel();}public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stubif(e.getSource()==addButton){this.CalculateEventHandel();result=userFloata+userFloatb;// System.out.println(""+result);textField3.setText(""+result);}else if(e.getSource()==subtractButton){ this.CalculateEventHandel();result=userFloata-userFloatb;textField3.setText(""+result);}else if(e.getSource()==rideButton){this.CalculateEventHandel();result=userFloata*userFloatb;textField3.setText(""+result);}else if(e.getSource()==divideButton){this.CalculateEventHandel();result=userFloata/userFloatb;textField3.setText(""+result);}}}。
Java程序设计计算器含代码设计
Java程序课程设计任务书实用性计算器的设计与开发1、主要内容:开发一个实用型的计算器程序,实现基本的计算功能同时并进行相应的功能拓展,使其具更加人性化的功能。
我们可以用其进行相应的计算功能来方便我们的学习,代替我们进行一些简单的脑力劳动。
2、具体要求(包括技术要求等):系统的功能要求:1.可以使用计算器进行简单的四则运算:用界面上的按钮输入数字和相应的运算符,即可算出相应的式子的答案,并且确保答案符合一定的精度要求;2.可以使用计算器进行一定的科学运算,比如在输入数字后,计算器可以将相应的数字进行进制转化,实现功能的同时确保一定的精度要求;3.可以进行简单的函数运算,比如求一个数的三角函数等;4.具有一定的错误提示功能,比如输入一个数除以0,我们将输出错误的信息。
学习并掌握以下技术:Javax.swing,AWT,事件处理等熟练使用以下开发工具:Eclipse实现系统上述的功能。
3、进度安排:12月28日~ 12月29日:课程设计选题,查找参考资料12月30日~ 12月31日:完成需求分析1月1日~ 1月5日:完成系统设计,完成程序代码的编写1月6日~ 1月6日:系统测试与完善1月7日~ 1月8日:完成课程设计报告,准备答辩四、主要参考文献[1] 耿祥义. JA V A课程设计[M]. 北京:清华大学出版社,2008年11月.[2] 张广彬, 孟红蕊, 张永宝. Java课程设计案例精编[M]. 北京:清华大学出版社,2007年.[3] 杨树林,胡洁萍. JA V A语言最新实用案例教程[M]. 北京:清华大学出版社,2006年1月. 45-170[4] 刘新Java开发技术大全北京[M].清华大学出版社2009年11月.[5] 黄明, 梁旭, 周绍斌编著. Java课程设计[M] 北京:电子工业出版社, 2006年目录JA V A程序课程设计任务书 (I)目录 (III)第1章引言 (1)1.1背景 (1)1.2课程设计内容 (1)1.3任务分工 (2)第2章实用型计算器的设计 (3)2.1需求分析 (3)2.2开发及运行环境 (3)2.3主要功能要求 (4)2.4系统模块化分析 (4)2.4.1 计算器主界面布局 (4)2.4.2实用型计算器各模块介绍 (7)2.5本章小结 (7)第3章计算器的具体实现 (8)3.1界面设计 (8)3.1.1用户登陆界面图 (8)3.1.2注册用户界面 (10)3.1.3 系统主界面 (12)3.1.4历史记录界面 (14)3.1.5 用户信息界面 (15)3.2程序设计及调试运行 (15)3.3本章小结 (32)第4章结束语 (33)致谢 (33)参考文献 (34)附录源代码 (34)第1章引言1.1 背景Java不依赖平台的特点使它受到广泛的关注,Java已成为网络时代最重要的语言之一。