




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
GDOU-B-11-112广东海洋大学学生实验报告书(学生用表)实验名称JAVA计算器程序设计课程名称JAVA程序设计课程号学院(系)专业班级学生姓名学号实验地点完成日期实验JAVA计算器程序设计1) 实验总结:刚做之前,勉强还能做些小程序,但在进行了计算器程序设计的时候,才发现很多语句还不能联系运用,大部分还是通过百度和书籍才勉勉强强完成,看来实践才是硬道理2)实验目的通过计算器程序设计,能够更加深入的了解JAVA,熟悉图形用户的设计原理和程序结构等4) 实验内容和步骤实验内容 应用JAVA软件设计一个简易的计算器,实现基本的四则运算及括号设计流程1. 分析该计算器需要完成的功能。 用户能够完成添加括号和负号的四则混合运算,并且计算器能够自动识别运算符的优先级,根据用户输入的运算表达式,自动计算出相应的结果。同时还完成了计算器中C按钮清屏功能和Backspace退格键。2. 考虑异常处理。 (1)当输入的表达式中出现除零的操作,显示框将显示“Infinity(无穷大)”。 (2)当输入的表达式错误时,将弹出提示框显示“表达式错误请重新输入” (3)当计算器的显示文本框里为没有输入内容时直接点击等号按钮,将弹出提示框显示“对不起,您没有任何输入,请重新输入:”。 (4)当第一次输入乘号键,除号键,右括号,小数点,求余键,等号键,ce键,Backspace键时提示错误。4. 考虑数据的处理 目标计算表达式通过点击计算器界面上的按钮进行输入,当按下等号之后,计算器通过调用JAVA中script这个包中的相关类和方法来实现数据的处理。5. 编码实现计算器的功能。 (1)新建相关的文件。 (2)引入JAVA中相关的包。 (3)新建类Jsq,并继承了类Frame,而且实现了接口ActionListener (4)定义相关的变量,创建相关组件,并对组件的属性进行设置。 (5)对所创建的组件进行布局,完成界面的实现。 (6)为各个组件添加事件监听器。 (7)重写事件接口ActionListener的方法public void actionPerformed(ActionEvent e)。 (8)为各个组件编写事件代码,完成每个按钮的不同功能。4、程序import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.awt.List;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class Calculator extends Framepublic static void main(String args) final Frame window = new Frame(Calculator); window .setSize(380, 337); window .setResizable(false); int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; window.setLocation(width / 2 - 200, height / 2 - 150); window .setLayout(null); /final JTextField text = new JTextField();text.setHorizontalAlignment(JTextField.RIGHT); /text.setEditable(false);text.setEditable(true); text.setBounds(10,40,360,50); window .add(text); text.setForeground(Color.blue); text.setBackground(Color.LIGHT_GRAY ); text.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) ScriptEngineManager sem1 = new ScriptEngineManager(); ScriptEngine se1 = sem1.getEngineByName(js);tryint leng10 = text.getText().length();if(leng10 = 0)JOptionPane.showMessageDialog(null, 对不起,您没有任何输入,请重新输入:); text.requestFocusInWindow(); else String reslut = se1.eval(text.getText().toString(); text.setText(text.getText()+=+reslut); text.requestFocus(); catch (ScriptException evt) JOptionPane.showMessageDialog(null,表达式错误,请重新输入!); text.setText(); /给文本框赋值为空达到清屏作用 text.requestFocus(); );/*设置0键*/final JButton b0 = new JButton(0); /新建按钮0b0.setBounds(10,283,72,50); /设置0键的位置window.add(b0); /把文本框添加到窗口中b0.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+0); text.requestFocus(); );/*设置1键*/final JButton b1 = new JButton(1); window.add(b1); b1.setBounds(10,233,72,50); b1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+1); text.requestFocus(); );/*设置2键*/final JButton b2 = new JButton(2);b2.setBounds(82,233,72,50); window.add(b2); b2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+2);text.requestFocus(); );/*设置3键*/final JButton b3 = new JButton(3);b3.setBounds(154,233,72,50); window.add(b3); b3.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+3); text.requestFocus(); );/*设置4键*/final JButton b4 = new JButton(4);b4.setBounds(10,183,72,50); window.add(b4); b4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+4); text.requestFocus(); );/*设置5键*/final JButton b5 = new JButton(5);b5.setBounds(82,183,72,50); window.add(b5); b5.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+5);text.requestFocus(); );/*设置6键*/final JButton b6 = new JButton(6);b6.setBounds(154,183,72,50); window.add(b6); b6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+6);text.requestFocus(); );/*设置7键*/final JButton b7 = new JButton(7);b7.setBounds(10,135,72,50); window.add(b7); b7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+7);text.requestFocus(); );/*设置8键*/final JButton b8 = new JButton(8);b8.setBounds(82,135,72,50); window.add(b8); b8.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+8);text.requestFocus(); );/*设置9键*/final JButton b9 = new JButton(9);b9.setBounds(154,135,72,50); window.add(b9); b9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+9);text.requestFocus(); );/*设置加法键*/final JButton Add = new JButton(+);Add.setBounds(154,283,72,50); window.add(Add); Add .addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+); text.requestFocus(); );/*设置减法键*/final JButton Sub = new JButton(-);Sub.setBounds(298,183,80,50); /设置减法键的位置window.add(Sub); /把减号键添加到窗口中 Sub.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+-); text.requestFocus(); );/*设置乘法键*/final JButton Multi = new JButton(*);Multi.setBounds(226,183,72,50); window.add(Multi); Multi.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int leng3 = text.getText().length(); if(leng3 = 1 ) text.setText(text.getText()+*); text.requestFocus(); else JOptionPane.showMessageDialog(null,您输入的第一个字符为乘号,不符合要求请重新输入:); text.requestFocus(); );/*设置除法键*/final JButton Div= new JButton(/);Div.setBounds(226,135,72,50); window.add(Div); Div.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int leng4 = text.getText().length(); if(leng4 = 1 ) text.setText(text.getText()+/); text.requestFocus(); else JOptionPane.showMessageDialog(null,您输入的第一个字符为除号,不符合要求请重新输入:); text.requestFocus(); );/*设置等号键*/final JButton equal = new JButton(=);equal.setBounds(226,283,153,50); window.add(equal); equal .addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName(js);tryint leng5 = text.getText().length();if(leng5=0)JOptionPane.showMessageDialog(null, 对不起,您没有任何输入,请重新输入:); text.requestFocus(); else String reslut = se.eval(text.getText().toString();text.setText(text.getText()+=+reslut);text.requestFocus(); catch (ScriptException evt) JOptionPane.showMessageDialog(null,表达式错误,请重新输入!); text.setText(); text.requestFocus(); );/*设置小数点键*/final JButton point = new JButton(.);point.setBounds(82,283,72,50); window.add(point); point.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int leng6 = text.getText().length(); if(leng6 0) text.setText(text.getText()+.); text.requestFocus(); else JOptionPane.showMessageDialog(null,您输入的第一个字符为小数点,不符合要求请重新输入:); text.requestFocus(); );/*设置左括号键*/final JButton left = new JButton();left .setBounds(226,233,72,50); /设置左括号键的位置window.add(left); /把左括号键添加到窗口中 left.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text.setText(text.getText()+();text.requestFocus(); );/*设置右括号键*/final JButton right = new JButton();right.setBounds(298,233,80,50); /设置右括号键的位置window.add(right); /把右括号键添加到窗口中 right.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int leng7 = text.getText().length(); if(leng7 0) text.setText(text.getText()+); text.requestFocus(); else JOptionPane.showMessageDialog(null,您输入的第一个字符为右括号,不符合要求请重新输入:); text.requestFocus(); );/*设置清屏键*/final JButton ce = new JButton(CE);ce.setBounds(190,90,186,45); window.add(ce); ce.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int leng8 = text.getText().length(); if(leng8 0 ) text.setText(); text.requestFocus(); else JOptionPane.showMessageDialog(null,当前以经为空,无需清屏!); text.requestFocus(); );/*设置退格键*/final JButton back = new JButton(Backspace);back.setBo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025辽宁抚顺市城建集团招聘拟聘用人员模拟试卷及一套完整答案详解
- 2025年福建福路通城乡发展集团有限公司招聘考前自测高频考点模拟试题及答案详解(易错题)
- 2025江苏南京千星赞科技发展有限公司模拟试卷完整答案详解
- 2025北京市大兴区垡上中学招聘初中数学1人模拟试卷有答案详解
- 2025内蒙古自治区农牧业科学院招聘控制数人员93人模拟试卷及答案详解(网校专用)
- 2025年中国吉他拾音器线圈行业市场分析及投资价值评估前景预测报告
- 2025年济南市章丘区卫生健康局所属事业单位公开招聘工作人员(116人)模拟试卷及完整答案详解1套
- 2025年陕西邮电职业技术学院招聘(4人)考前自测高频考点模拟试题及答案详解(名师系列)
- 2025广东粤电大埔发电有限公司招聘16人模拟试卷及一套答案详解
- 2025年4月重庆市綦江区万东镇新招录公益性岗位人员考前自测高频考点模拟试题附答案详解(考试直接用)
- 水下砼切割施工方案
- v60呼吸机的使用与维护
- 论语全文带拼音
- 黄花菜种植技术
- 温病常用诊法舌诊课件
- 盆腔炎性疾病和生殖器结核专家讲座
- DISC性格测评问卷及分析
- 日本蜡烛图技术完整版
- 苏教版数学四年级上册《解决问题的策略》课件
- LY/T 1571-2000国有林区营造林检查验收规则
- 内分泌和代谢疾病总论课件
评论
0/150
提交评论