1、java计算器实验报告GDOU-B-11-112广东海洋大学学生实验报告书(学生用表)实验名称JAVA计算器程序设计课程名称JAVA程序设计课程号学院(系)专业班级学生姓名学号实验地点完成日期实验JAVA计算器程序设计1) 实验总结:刚做之前,勉强还能做些小程序,但在进行了计算器程序设计的时候,才发现很多语句还不能联系运用,大部分还是通过XX和书籍才勉勉强强完成,看来实践才是硬道理2)实验目的通过计算器程序设计,能够更加深入的了解JAVA,熟悉图形用户的设计原理和程序结构等4)实验内容和步骤实验内容 应用JAVA软件设计一个简易的计算器,实现基本的四则运算及括号设计流程1. 分析该计算器需要完
2、成的功能。 用户能够完成添加括号和负号的四则混合运算,并且计算器能够自动识别运算符的优先级,根据用户输入的运算表达式,自动计算出相应的结果。同时还完成了计算器中C按钮清屏功能和Backspace退格键。2. 考虑异常处理。 (1)当输入的表达式中出现除零的操作,显示框将显示“Infinity(无穷大)”。 (2)当输入的表达式错误时,将弹出提示框显示“表达式错误请重新输入” (3)当计算器的显示文本框里为没有输入内容时直接点击等号按钮,将弹出提示框显示“对不起,您没有任何输入,请重新输入:”。 (4)当第一次输入乘号键,除号键,右括号,小数点,求余键,等号键,ce键,Backspace键时提示
3、错误。4. 考虑数据的处理 目标计算表达式通过点击计算器界面上的按钮进行输入,当按下等号之后,计算器通过调用JAVA中script这个包中的相关类和方法来实现数据的处理。5. 编码实现计算器的功能。 (1)新建相关的文件。 (2)引入JAVA中相关的包。 (3)新建类Jsq,并继承了类Frame,而且实现了接口ActionListener (4)定义相关的变量,创建相关组件,并对组件的属性进行设置。 (5)对所创建的组件进行布局,完成界面的实现。 (6)为各个组件添加事件监听器。 (7)重写事件接口ActionListener的方法public void actionPerformed(Act
4、ionEvent 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 Fram
5、e public 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 - 20
6、0, 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_
7、GRAY ); text.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) ScriptEngineManager sem1 = new ScriptEngineManager(); ScriptEngine se1 = sem1.getEngineByName(js); try int leng10 = text.getText().length(); if(leng10 = 0) JOptionPane.showMessageDialog(null, 对不起,您没有任何输
8、入,请重新输入:); 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键*/
9、final JButton b0 = new JButton(0); /新建按钮0 b0.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.a
10、dd(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 ActionList
11、ener() 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.ge
12、tText()+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 JBu
13、tton(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.addActionListe
14、ner(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) tex
15、t.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 JBu
16、tton 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(A
17、dd); 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 ActionListen
18、er() 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
19、 = 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
20、(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 JBu
21、tton(=); 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); try int leng5 = text.getText().length(); if(leng5=0) J
22、OptionPane.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.requestF
23、ocus(); ); /*设置小数点键*/ 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();
24、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)
25、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(); ); /*设置清屏键*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1