广东海洋大学学生实验报告书
实验名称: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 Frame
{
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 - 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");
try{
int 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"); //新建按钮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.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");
try{
int 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.setBounds(10,90,180,45);
window.add(back);
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String message;
int leng = text.getText().length();
message=text.getText();
if(leng==0)
{
JOptionPane.showMessageDialog(null, "对不起,您没有任何输入无法退格:");
text.requestFocus();
}
else
{
text.setText(message.substring(0,leng-1));
text.requestFocus();
}
}
});
final JButton Air = new JButton("%");
Air.setBounds(298,135,80,50);
window.add(Air);
Air.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int leng9 = text.getText().length();
{
if(leng9 > 0)
{
text.setText(text.getText()+"%");
text.requestFocus();
}
else
{
JOptionPane.showMessageDialog(null,"您输入的第一个字符为求余数,不符合要求请重新输入:");
text.requestFocus();
}
}
}
});
/*********关闭按钮***********/
window .addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
window .show();
}
}
程序截图
1.初始运行界面
2.运行测试