java计算器实验报告Word下载.docx
《java计算器实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《java计算器实验报告Word下载.docx(18页珍藏版)》请在冰豆网上搜索。
设计流程
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的方法publicvoidactionPerformed(ActionEvente)。
(8)为各个组件编写事件代码,完成每个按钮的不同功能。
4、程序
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.List;
importjavax.script.ScriptEngine;
importjavax.script.ScriptEngineManager;
importjavax.script.ScriptException;
publicclassCalculatorextendsFrame
{
publicstaticvoidmain(String[]args)
{
finalFramewindow=newFrame("
Calculator"
);
window.setSize(380,337);
window.setResizable(false);
intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;
intheight=Toolkit.getDefaultToolkit().getScreenSize().height;
window.setLocation(width/2-200,height/2-150);
window.setLayout(null);
/
finalJTextFieldtext=newJTextField();
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(newActionListener()
{
publicvoidactionPerformed(ActionEventarg0){
ScriptEngineManagersem1=newScriptEngineManager();
ScriptEnginese1=sem1.getEngineByName("
js"
try{
intleng10=text.getText().length();
if(leng10==0)
{
JOptionPane.showMessageDialog(null,"
对不起,您没有任何输入,请重新输入:
"
text.requestFocusInWindow();
}
else{
Stringreslut=se1.eval(text.getText()).toString();
text.setText(text.getText()+"
="
+reslut);
text.requestFocus();
}
}catch(ScriptExceptionevt){
JOptionPane.showMessageDialog(null,"
表达式错误,请重新输入!
text.setText("
//给文本框赋值为空达到清屏作用
text.requestFocus();
}
}
});
/***********设置0键***************/
finalJButtonb0=newJButton("
0"
//新建按钮0
b0.setBounds(10,283,72,50);
//设置0键的位置
window.add(b0);
//把文本框添加到窗口中
b0.addActionListener(newActionListener()
publicvoidactionPerformed(ActionEvente)
{
text.setText(text.getText()+"
text.requestFocus();
/********设置1键****************/
finalJButtonb1=newJButton("
1"
window.add(b1);
b1.setBounds(10,233,72,50);
b1.addActionListener(newActionListener()
/***********设置2键*****************/
finalJButtonb2=newJButton("
2"
b2.setBounds(82,233,72,50);
window.add(b2);
b2.addActionListener(newActionListener()
{
/********设置3键****************/
finalJButtonb3=newJButton("
3"
b3.setBounds(154,233,72,50);
window.add(b3);
b3.addActionListener(newActionListener()
/*********设置4键***************/
finalJButtonb4=newJButton("
4"
b4.setBounds(10,183,72,50);
window.add(b4);
b4.addActionListener(newActionListener()
{
/*********设置5键*************/
finalJButtonb5=newJButton("
5"
b5.setBounds(82,183,72,50);
window.add(b5);
b5.addActionListener(newActionListener()
/********设置6键***************/
finalJButtonb6=newJButton("
6"
b6.setBounds(154,183,72,50);
window.add(b6);
b6.addActionListener(newActionListener()
/*********设置7键**************/
finalJButtonb7=newJButton("
7"
b7.setBounds(10,135,72,50);
window.add(b7);
b7.addActionListener(newActionListener()
/**********设置8键***************/
finalJButtonb8=newJButton("
8"
b8.setBounds(82,135,72,50);
window.add(b8);
b8.addActionListener(newActionListener()
/***********设置9键**************/
finalJButtonb9=newJButton("
9"
b9.setBounds(154,135,72,50);
window.add(b9);
b9.addActionListener(newActionListener()
/***********设置加法键**************/
finalJButtonAdd=newJButton("
+"
Add.setBounds(154,283,72,50);
window.add(Add);
Add.addActionListener(newActionListener()
publicvoidactionPerformed(ActionEvente)
text.setText(text.getText()+"
text.requestFocus();
/***********设置减法键**************/
finalJButtonSub=newJButton("
-"
Sub.setBounds(298,183,80,50);
//设置减法键的位置
window.add(Sub);
//把减号键添加到窗口中
Sub.addActionListener(newActionListener()
/***********设置乘法键**************/
finalJButtonMulti=newJButton("
*"
Multi.setBounds(226,183,72,50);
window.add(Multi);
Multi.addActionListener(newActionListener()
intleng3=text.getText().length();
if(leng3>
=1)
else{
JOptionPane.showMessageDialog(null,"
您输入的第一个字符为乘号,不符合要求请重新输入:
/***********设置除法键**************/
finalJButtonDiv=newJButton("
/"
Div.setBounds(226,135,72,50);
window.add(Div);
Div.addActionListener(newActionListener()
intleng4=text.getText().length();
if(leng4>
您输入的第一个字符为除号,不符合要求请重新输入:
/***********设置等号键**************/
finalJButtonequal=newJButton("
equal.setBounds(226,283,153,50);
window.add(equal);
equal.addActionListener(newActionListener()
ScriptEngineManagersem=newScriptEngineManager();
ScriptEnginese=sem.getEngineByName("
intleng5=text.getText().length();
if(leng5==0)
text.requestFocus();
Stringreslut=se.eval(text.getText()).toString();
/***********设置小数点键**************/
finalJButtonpoint=newJButton("
."
point.setBounds(82,283,72,50);
window.add(point);
point.addActionListener(newActionListener()
intleng6=text.getText().length();
if(leng6>
0)
您输入的第一个字符为小数点,不符合要求请重新输入:
/***********设置左括号键**************/
finalJButtonleft=newJButton("
("
left.setBounds(226,233,72,50);
//设置左括号键的位置
window.add(left);
//把左括号键添加到窗口中
left.addActionListener(newActionListener()
/***********设置右括号键**************/
finalJButtonright=newJButton("
)"
right.setBounds(298,233,80,50);
//设置右括号键的位置
window.add(right);
//把右括号键添加到窗口中
right.addActionListener(newActionListener()
intleng7=text.getText().length();
if(leng7>
else
您输入的第一个字符为右括号,不符合要求请重新输入:
/***********设置清屏键**************/
fina