JAVA 课程设计
实验报告
题 目: 保存计算过程的计算器
组 别: 第
组
姓 名: 李治明
学 号: 2017211327
班 级: 软件工程十班
重庆邮电大学移通学院
2018 年 1 月
一、概述
题目:保存计算过程的计算器
要求:参考 windows 操作系统提供的计算器设计一个实用的计算器,要求除了具有普通的计算器功能外,
还具有保存计算器过程的功能。
1.点击计算器上的数字按钮(0、1、2、3、4、5、6、7、8、9)可以设置参与计算的运算数。
2.点击计算器上的运算符按钮(+、-、*、/)可以选择运算符号。
3.点击计算器上的函数按钮可以计算出相应的函数值。
4.点击计算器上的等号(=)按钮显示计算结果。
5.在一个文本框中显示当前的计算过程,在一个文本区中显示以往的计算过程。
6.点击“保存”按钮可以将文本区中显示的全部计算过程保存到文件;点击“复制”按钮可以将文本区中
选中的文本复制到剪贴板;点击“清徐”按钮可以清除文本区中的全部内容。
二、功能模块描述含相应代码
2.1 导入包
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList;
import java.io.*;
2.2 各模块代码
2.2.1 HandleBack 类
HandleBack 类实现了 ActionListener 接口,创建的对象 handleBack 是 CalculatorWindow 窗口的成
员之一,该类实现了退格算法。
使用到如下代码进行退格判定和操作:
if(num.length()>=1){
num=num.substring(0,num.length()-1);
list.set(0,num);
resultShow.setText(num);
showComputerProcess.setText(""+num);
}
else{
list.removeLast();
resultShow.setText("0");
showComputerProcess.setText("0");
1
}
该 类 中 包 含 了 成 员 变 量 : list 、 resultShow 、 showComputerProcess ; 包 含 方 法 : 构 造 方 法
HandleBack(LinkedList,JtextField,JtextField)、HandleBack 类实现的 ActionListener 接口
中的方法。
2.2.2 HandleClear 类
HandleClear 类实现了 ActionListener 接口,创建的对象 handleBack 是 CalculatorWindow 窗口的成
员之一,该类实现了清零算法。
使用到如下代码进行清零:
resultShow.setText("0");
list.clear();
showComputerProcess.setText(null);
该 类 中 包 含 了 成 员 变 量 : list 、 resultShow 、 showComputerProcess ; 包 含 方 法 : 构 造 方 法
HandleClear(LinkedList,JtextField,JtextField)、HandleClear 类实现的 ActionListener 接
口 中 的 方 法 。 当 用 户 点 击 “ 清 零 操 作 ” 按 钮 时 就 会 触 发 ActionEvent 事 件 , 执 行
actionPerformed(ActionEvent)方法,将 resultShow 中显示的数字设置为 0,并清除 showComputerProcess
中显示的计算过程。
2.2.3 HandleDot 类
HandleDot 类实现了 ActionListener 接口,创建的对象 handleDot 是 CalculatorWindow 窗口的成员
之一,该类实现了处理小数点算法。
使用到如下代码进行小数点的判定和处理:
String dot=e.getActionCommand();
String num=list.getFirst();
String s=null;
if(num.indexOf(dot)==-1){
s=num.concat(dot);
list.set(0,s);
}
else{
s=num;
list.set(0,s);
resultShow.setText(s);
showComputerProcess.setText(""+list.get(0));
}
2.2.4 HandleDigit 类
HandleDigit 类实现了 ActionListener 接口,创建的对象 handleDigit 是 CalculatorWindow 窗口的
成员之一,该类实现了获取数字的算法。
通过此语句获得点击的数字:
NumberButton b=(NumberButton)e.getSource();
传递、显示点击的数字:
int number=b.getNumber();
list.add(""+number);
resultShow.setText(""+number);
showComputerProcess.setText(""+list.get(0));
2.2.5 HandleOperation 类
2
HandleOperation 类实现了 ActionListener 接口,创建的对象 handleOperation 是 CalculatorWindow
窗口的成员之一,该类实现了获取符号的算法。
通过此语句获得点击的符号:
OperationButton b=(OperationButton)e.getSource();
通过如下代码进行加减乘除的判断和运算:
if(运算符号.equals("+"))
result=n1+n2;
else if(运算符号.equals("-"))
result=n1-n2;
else if(运算符号.equals("*"))
result=n1*n2;
else if(运算符号.equals("/"))
result=n1/n2;
该 类 中 包 含 了 成 员 变 量 : list 、 resultShow 、 showComputerProcess ; 包 含 方 法 : 构 造 方 法
HandleOperation(LinkedList,JtextField,JtextField) 、 HandleOperation 类 实 现 的
ActionListener 接 口 中 的 方 法 。 当 用 户 点 击 运 算 符 按 钮 时 就 会 触 发 ActionEvent 事 件 , 执 行
actionPerformed(ActionEvent)方法,负责处理 list 链表中存储的运算符和必要的计算。
2.2.6 HandleSin 类
HandleSin 类实现了 ActionListener 接口,创建的对象 handleSin 是 CalculatorWindow 窗口的成员
之一,该类实现了正弦函数值算法。
当 list.size()==1||list.size()==2 时,需要算法:
double x=Double.parseDouble(numOne);
double result=Math.sin(x);
String str=String.valueOf(result);
list.set(0,str);
resultShow.setText(str);
list.removeLast(); //移掉运算符号
当 list.size()==3)时,需要算法:
String numTwo=list.getLast();
double x=Double.parseDouble(numTwo);
double result=Math.sin(x);
String str=String.valueOf(result);
list.set(0,str);
resultShow.setText(str);
list.removeLast(); //移掉第 2 个运算数
list.removeLast(); //移掉运算符号
该 类 中 包 含 了 成 员 变 量 : list 、 resultShow 、 showComputerProcess ; 包 含 方 法 : 构 造 方 法
HandleSin(LinkedList,JtextField,JtextField)、HandleSin 类实现的 ActionListener 接口中
的方法。当用户点击“Sin”按钮时就会触发 ActionEvent 事件,执行 actionPerformed(ActionEvent)方
法,将输入的数字的正弦函数值计算出来。
2.3 全部代码
package 计算机;
import java.awt.*;
import java.awt.event.*;
3
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList;
import java.io.*;
public class CalculatorWindow extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
NumberButton numberButton[];
OperationButton operationButton[];
JButton 小数点操作,正负号操作,退格操作,等号操作,清零操作,sin;
JTextField resultShow;
//显示计算结果
JTextField showComputerProcess; //显示当前计算过程
JTextArea saveComputerProcess; //显示计算步骤
JButton saveButton,copyButton,clearButton;
LinkedList list;
//链表用来存放第一个运算数、运算符号和第二个运算数
HandleDigit handleDigit; //负责处理 ActionEvent 事件
HandleOperation handleOperation ;
HandleBack handleBack;
HandleClear handleClear;
HandleEquality handleEquality;
HandleDot handleDot;
HandlePOrN handlePOrN;
HandleSin handleSin;
public CalculatorWindow(){
setTitle("计算器");
JPanel panelLeft,panelRight;
list=new LinkedList();
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.BOLD,16));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setEditable(false);
resultShow.setBackground(Color.white);
showComputerProcess=new JTextField();
showComputerProcess.setHorizontalAlignment(JTextField.CENTER);
showComputerProcess.setFont(new Font("Arial",Font.BOLD,16));
showComputerProcess.setBackground(Color.cyan);
showComputerProcess.setEditable(false);
saveComputerProcess=new JTextArea();
saveComputerProcess.setEditable(false);
saveComputerProcess.setFont(new Font("宋体",Font.PLAIN,16));
4
numberButton=new NumberButton[10];
handleDigit=new HandleDigit(list,resultShow,showComputerProcess);
for(int i=0;i<=9;i++){
numberButton[i]=new NumberButton(i);
numberButton[i].setFont(new Font("Arial",Font.BOLD,20));
numberButton[i].addActionListener(handleDigit);
}
operationButton=new OperationButton[4];
handleOperation=new HandleOperation(list,resultShow,
showComputerProcess,saveComputerProcess);
String 运算符号[]={"+","-","*","/"};
for(int i=0;i<4;i++){
operationButton[i]=new OperationButton(运算符号[i]);
operationButton[i].setFont(new Font("Arial",Font.BOLD,20));
operationButton[i].addActionListener(handleOperation);
}
小数点操作=new JButton(".");
handleDot=new HandleDot(list,resultShow,showComputerProcess);
小数点操作.addActionListener(handleDot);
正负号操作=new JButton("+/-");
handlePOrN=new HandlePOrN(list,resultShow,showComputerProcess);
正负号操作.addActionListener(handlePOrN);
等号操作=new JButton("=");
handleEquality=new HandleEquality(list,resultShow,
showComputerProcess,saveComputerProcess);
等号操作.addActionListener(handleEquality);
sin=new JButton("sin");
handleSin=new HandleSin(list,resultShow,
showComputerProcess,saveComputerProcess);
sin.addActionListener(handleSin);
退格操作=new JButton("退格");
handleBack=new HandleBack(list,resultShow,showComputerProcess);
退格操作.addActionListener(handleBack);
清零操作=new JButton("C");
handleClear=new HandleClear(list,resultShow,showComputerProcess);
清零操作.addActionListener(handleClear);
清零操作.setForeground(Color.red);
退格操作.setForeground(Color.red);
等号操作.setForeground(Color.red);
sin.setForeground(Color.blue);
正负号操作.setForeground(Color.blue);
小数点操作.setForeground(Color.blue);
panelLeft=new JPanel();
panelRight=new JPanel();
5
panelLeft.setLayout(new BorderLayout());
JPanel centerInLeft=new JPanel();
panelLeft.add(resultShow,BorderLayout.NORTH);
panelLeft.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(4,5));
centerInLeft.add(numberButton[1]);
centerInLeft.add(numberButton[2]);
centerInLeft.add(numberButton[3]);
centerInLeft.add(operationButton[0]);
centerInLeft.add(清零操作);
centerInLeft.add(numberButton[4]);
centerInLeft.add(numberButton[5]);
centerInLeft.add(numberButton[6]);
centerInLeft.add(operationButton[1]);
centerInLeft.add(退格操作);
centerInLeft.add(numberButton[7]);
centerInLeft.add(numberButton[8]);
centerInLeft.add(numberButton[9]);
centerInLeft.add(operationButton[2]);
centerInLeft.add(sin);
centerInLeft.add(numberButton[0]);
centerInLeft.add(正负号操作);
centerInLeft.add(小数点操作);
centerInLeft.add(operationButton[3]);
centerInLeft.add(等号操作);
panelRight.setLayout(new BorderLayout());
panelRight.add(showComputerProcess,BorderLayout.NORTH);
saveButton=new JButton("保存");
copyButton=new JButton("复制");
clearButton=new JButton("清除");
saveButton.setToolTipText("保存计算过程到文件");
copyButton.setToolTipText("复制选中的计算过程");
clearButton.setToolTipText("清除计算过程");
saveButton.addActionListener(this);
copyButton.addActionListener(this);
clearButton.addActionListener(this);
panelRight.add(new
JScrollPane(saveComputerProcess),BorderLayout.CENTER);
JPanel southInPanelRight=new JPanel();
southInPanelRight.add(saveButton);
southInPanelRight.add(copyButton);
southInPanelRight.add(clearButton);
panelRight.add(southInPanelRight,BorderLayout.SOUTH);
JSplitPane split=new JSplitPane
6
(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,50,528,258);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==copyButton)
saveComputerProcess.copy();
if(e.getSource()==clearButton)
saveComputerProcess.setText(null);
if(e.getSource()==saveButton){
JFileChooser chooser=new JFileChooser();
int state=chooser.showSaveDialog(null);
File file=chooser.getSelectedFile();
if(file!=null&&state==JFileChooser.APPROVE_OPTION){
try{ String content=saveComputerProcess.getText();
StringReader read=new StringReader(content);
BufferedReader in= new BufferedReader(read);
FileWriter outOne=new FileWriter(file);
BufferedWriter out= new BufferedWriter(outOne);
String str=null;
while((str=in.readLine())!=null){
out.write(str);
out.newLine();
}
in.close();
out.close();
}
catch(IOException e1){}
}
}
}
public class NumberButton extends JButton{
/**
*
*/
private static final long serialVersionUID = 1L;
int number;
public NumberButton(int number){
super(""+number);
this.number=number;
setForeground(Color.blue);
7