Java课程设计保存过程的计算器Word文档下载推荐.docx
《Java课程设计保存过程的计算器Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Java课程设计保存过程的计算器Word文档下载推荐.docx(47页珍藏版)》请在冰点文库上搜索。
、NumberButton、OperationButton、JButton、JTextField、JTextArea、HandleDigit、HandleOperation、HandleDot、HandleEquality、HandleSin、HandleBack、HandleClear、HandlePOrN对象。
CalcuatorWindow类创建的窗口以及其中的主要成员如图2.2所示。
CalculatorWindow类的主要成员的作用将在后面的详细设计中阐述。
图2.2CalculatorWindow窗口及主要的对象成员
2.2NumberButton类设计
NumberButton类创建的对象是主类CalculatorWindow窗口中的一个“数字”按钮。
NumberButton类有一个int类型的成员number,标明所创建的按钮所含有的数字。
NumberButton所创建的10个按钮被添加到CalcuatorWindow窗口中。
2.3OperationButton类设计
OperationButton类创建的对象是主类CalculatorWindow窗口中的一个“运算符”按钮。
OperationButton类有一个String类型的成员operateSign,用于标明所创建的按钮所含有的运算符号。
OperationButton所创建的4个按钮被添加到CalculatorWindow窗口中。
2.4HandleDigit类设计
HandleDigit类创建的对象负责处理ActionEvent事件。
当用户单击“数字”按钮时,HandleDigit类所创建的对象负责处理和数字有关的计算过程。
2.5HandleOperation类设计
HandleOperation类创建的对象负责处理ActionEvent事件,当用户单击“运算符”按钮时,HandleOperation类所创建的对象负责处理和运算符有关的计算过程。
2.6HandleDot类设计
HandleDot类创建的对象负责处理ActionEvent事件,当用户单击“.”按钮(小数点)时,HandleDot类所创建的对象负责处理小数点。
2.7HandleEquality类设计
HandleEquality类创建的对象负责处理ActionEvent事件。
当用户单击“=”按钮时,HandleEquality类所创建的对象计算有关数据。
2.8HandleSin类设计
HandleSin类创建的对象负责处理ActionEvent事件。
当用户单击“sin”按钮(正弦三角函数)时,HandleSin类所创建的对象负责处理正弦三角函数的值。
2.9HandleBack类设计
HandleBack类创建的对象负责处理ActionEvent事件。
当用户单击“退格”按钮时,HandleBack类所创建的对象负责计算正弦三晋函数的值。
2.10HandleClear类设计
HandleClear所创建的对象负责处理ActionEvent事件。
当用户成功单击C按钮(清零)时,HandleClear类所创建的对象负责清除有关的计算结果。
2.11HandlePOrN类设计
HandlePOrN类创建的对象负责处理ActionEvent事件。
当用户单击“+/-”(正负)按钮时,HandlePOrN类所创建的对象负责处理数字的正负转换。
3详细设计
3.1CalculatorWindow类
1、效果图
CalculatorWindow创建的窗口效果如图3.1所示。
图3.1CalculatorWindow创建的窗口
2、UML图
CalculatorWindow类是javax.swing包中JFrame的一个子类,并实现了ActionListener接口。
标明该类的主要成员变量和方法的UML图如图3.2所示。
JFrame
ActionListener
CalculatorWindow
numberButton:
NumberButton[];
operationButton:
OperationButton[];
Flex,zhengfu,Backspace,Equals,Zero,sin:
JButton
resultShow,showComputerProcess:
JTextField
saveComputerProcess:
JTextArea
list:
LinkedList<
handleDigit:
HandleDigit
handleOperation:
HandleOperation
handleBack:
HandleBack
handleClear:
HandleClear
handleEquality:
HandleEquality
handleDot:
HandleDot
handlePOrN:
HandlePOrN
handleSin:
HandleSin
actionPerformed(ActionEvent):
void
CalculatorWindow():
无类型
main(String):
图3.2CalculatorWindow类的UML图
3、类设计
模块名称
功能描述
负责创建计算器的主窗口
接口与属性
VoidactionPerformed()
Function:
对saveButton,copyButton,clearButton三个按钮的ActionEvent事件监听
InPut:
无
OutPut:
Return:
空
CalculatorWindow()
Function:
是构造方法,负责完成窗口的初始化
无
StaticvoidMain()
Functon:
是计算器程序运行的入口方法
InPut:
OutPut:
Return:
数据结构
与算法
Linkedlist(String)list
补充说明
4、代码(CalculatorWindow.java)
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.*;
importjava.util.LinkedList;
importjava.io.*;
publicclassCalculatorWindowextendsJFrameimplementsActionListener{
NumberButtonnumberButton[];
OperationButtonoperationButton[];
JButtonFlex,zhengfu,Backspace,Equals,Zero,sin;
JTextFieldresultShow;
//显示计算结果
JTextFieldshowComputerProcess;
//显示当前计算过程
JTextAreasaveComputerProcess;
//显示计算步骤
JButtonsaveButton,copyButton,clearButton;
LinkedList<
list;
//链表用来存放第一个运算数、运算符号和第二个运算数
HandleDigithandleDigit;
//负责处理ActionEvent事件
HandleOperationhandleOperation;
HandleBackhandleBack;
HandleClearhandleClear;
HandleEqualityhandleEquality;
HandleDothandleDot;
HandlePOrNhandlePOrN;
HandleSinhandleSin;
publicCalculatorWindow(){
setTitle("
计算器计算机112班程逸群"
);
JPanelpanelLeft,panelRight;
list=newLinkedList<
();
resultShow=newJTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setForeground(Color.blue);
resultShow.setFont(newFont("
TimesRoman"
Font.BOLD,16));
resultShow.setBorder(newSoftBevelBorder(BevelBorder.LOWERED));
resultShow.setEditable(false);
resultShow.setBackground(Color.white);
showComputerProcess=newJTextField();
showComputerProcess.setHorizontalAlignment(JTextField.CENTER);
showComputerProcess.setFont(newFont("
Arial"
showComputerProcess.setBackground(Color.cyan);
showComputerProcess.setEditable(false);
saveComputerProcess=newJTextArea();
saveComputerProcess.setEditable(false);
saveComputerProcess.setFont(newFont("
宋体"
Font.PLAIN,16));
numberButton=newNumberButton[10];
handleDigit=newHandleDigit(list,resultShow,showComputerProcess);
for(inti=0;
i<
=9;
i++){
numberButton[i]=newNumberButton(i);
numberButton[i].setFont(newFont("
Font.BOLD,20));
numberButton[i].addActionListener(handleDigit);
}
operationButton=newOperationButton[4];
handleOperation=newHandleOperation(list,resultShow,
showComputerProcess,saveComputerProcess);
Stringyunsuan[]={"
+"
"
-"
*"
/"
};
4;
operationButton[i]=newOperationButton(yunsuan[i]);
operationButton[i].setFont(newFont("
operationButton[i].addActionListener(handleOperation);
Flex=newJButton("
."
handleDot=newHandleDot(list,resultShow,showComputerProcess);
Flex.addActionListener(handleDot);
zhengfu=newJButton("
+/-"
handlePOrN=newHandlePOrN(list,resultShow,showComputerProcess);
zhengfu.addActionListener(handlePOrN);
Equals=newJButton("
="
handleEquality=newHandleEquality(list,resultShow,
Equals.addActionListener(handleEquality);
sin=newJButton("
sin"
handleSin=newHandleSin(list,resultShow,
sin.addActionListener(handleSin);
Backspace=newJButton("
退格"
handleBack=newHandleBack(list,resultShow,showComputerProcess);
Backspace.addActionListener(handleBack);
Zero=newJButton("
C"
handleClear=newHandleClear(list,resultShow,showComputerProcess);
Zero.addActionListener(handleClear);
Zero.setForeground(Color.red);
Backspace.setForeground(Color.red);
Equals.setForeground(Color.red);
sin.setForeground(Color.blue);
zhengfu.setForeground(Color.blue);
Flex.setForeground(Color.blue);
panelLeft=newJPanel();
panelRight=newJPanel();
panelLeft.setLayout(newBorderLayout());
JPanelcenterInLeft=newJPanel();
panelLeft.add(resultShow,BorderLayout.NORTH);
panelLeft.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(newGridLayout(4,5));
centerInLeft.add(numberButton[1]);
centerInLeft.add(numberButton[2]);
centerInLeft.add(numberButton[3]);
centerInLeft.add(operationButton[0]);
centerInLeft.add(Zero);
centerInLeft.add(numberButton[4]);
centerInLeft.add(numberButton[5]);
centerInLeft.add(numberButton[6]);
centerInLeft.add(operationButton[1]);
centerInLeft.add(Backspace);
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(zhengfu);
centerInLeft.add(Flex);
centerInLeft.add(operationButton[3]);
centerInLeft.add(Equals);
panelRight.setLayout(newBorderLayout());
panelRight.add(showComputerProcess,BorderLayout.NORTH);
saveButton=newJButton("
保存"
copyButton=newJButton("
复制"
clearButton=newJButton("
清除"
saveButton.setToolTipText("
保存计算过程到文件"
copyButton.setToolTipText("
复制选中的计算过程"
clearButton.setToolTipText("
清除计算过程"
saveButton.addActionListener(this);
copyButton.addActionListener(this);
clearButton.addActionListener(this);
panelRight.add(newJScrollPane(saveComputerProcess),BorderLayout.CENTER);
JPanelsouthInPanelRight=newJPanel();
southInPanelRight.add(saveButton);
southInPanelRight.add(copyButton);
southInPanelRight.add(clearButton);
panelRight.add(southInPanelRight,BorderLayout.SOUTH);
JSplitPanesplit=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,panelLeft,panelRight);
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100,50,528,258);
validate();
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==copyButton){
saveComputerProcess.copy();
if(e.getSource()==clearButton){
saveComputerProcess.setText(null);
if(e.getSource()==saveButton){
Jchooser=newJ();
intstate=chooser.showSaveDialog(null);
=chooser.getSelectedFile();
if(null&
&
state==J){
try{
Stringcontent=saveComputerProcess.getText();
StringReaderread=newStringReader(content);
BufferedReaderin=newBufferedReader(read);
outOne=new(file);
BufferedWriterout=newBufferedWriter(outOne);
Stringstr=null;
while((str=in.readLine())!
=null){
out.write(str);
out.newLine();
in.close();
out.close();
}catch(IOExceptione1){
publicstaticvoidmain(Stringargs[]){
newCalculatorWindow();
}
3.2NumberButton类
NumberButton创建的数字按钮效果如图3.3所示。
图3.3NumberButton