JAVA课设计算器Word格式.docx
- 文档编号:8060620
- 上传时间:2023-05-10
- 格式:DOCX
- 页数:19
- 大小:152.15KB
JAVA课设计算器Word格式.docx
《JAVA课设计算器Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA课设计算器Word格式.docx(19页珍藏版)》请在冰点文库上搜索。
2.实现一些简单的扩展运算,如:
正负号、倒数、退格、清零等功能。
3.添加小数点功能,用以实现浮点型数据的计算。
4.使用布局管理器设计一个计算器的界面,使用事件监听器处理数据的输入,并完成相关的计算。
三、设计思路
1.界面设计:
(如图1)
图1
2.设计思路:
(1)根据所设计出来的界面,首先要设计其GUI界面,总体界面有一个文本框,20个按钮,总体界面用BorderLayout布局,文本框放置在最NORTH,然后0到9以及+,-,*,/等按钮放置到一个面板Panel中,完成界面设计。
(2)设计计算流程,首先点击数字按钮时,将按钮数值添加到文本框当中,并将该数值保存到一个字符串中,再次点击数字按钮时,将之前保存的字符串与新的数值拼接起来,再添加到文本框当中,直到点击运算符按钮时,将文本框当中的字符串保存在一个字符串变量中,然后重置文本框内容,将运算符号显示到文本框中,随后输入第二个计算数据时,用同样的办法保存数据,最后通过控制“=”运算符先将字符串数据转化成双精度类型,然后计算出结果并显示到文本框当中。
(3)基本运算设计完成以后则开始考虑其他个别功能的实现,例如倒数、清零、退格等功能的实现,清零直接重置文本框内容,退格功能则采用substring函数截取字符串长度。
四、详细设计
1.程序流程图:
2.主要代码展示及说明:
packagecn.ldxu;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassCalculationimplementsActionListener
{
doubledResult=0;
doubledNowInput=0;
doubledMemory;
intn=0;
//记载小数位数
intnOperation=1;
//记录运算符类型
intnBitsNum=0;
//记录总共输入的位数
booleanalreadyHaveDot=false;
//已经有小数点?
booleankeyAvailable=true;
booleanalreadyClickedEqueal=false;
//是否按下过"
="
?
booleanisTempNowInput=false;
//是否在计算出结果后直接按运算符将结果赋给了当前输入值?
JFramef;
JPanelp1,p2,p3,p4,p5,p6;
JTextFieldtf1,tf2;
JButtonb1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
JButtonbDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative;
JButtonbBackspace,bCE,bC,bMR,bMS,bMC,bM;
publicvoiddisplay()
{
f=newJFrame("
我的计算器"
);
f.setBounds(200,200,400,235);
f.setBackground(Color.LIGHT_GRAY);
f.setResizable(false);
f.setLayout(newBorderLayout(3,3));
p1=newJPanel(newGridLayout(1,3,5,5));
//用于存放backspace,ce,c三键
p2=newJPanel(newGridLayout(4,5,5,5));
//用于存放数字区及附近共20键,此处间隙设置可能不合理,以后调整
p3=newJPanel(newGridLayout(5,1,5,5));
//用于存放MC,MR,MS,M+键及显示M状态文本框,此处间隙设置可能不合理,以后调整
p4=newJPanel(newFlowLayout());
//用于存放p1,p2
p5=newJPanel(newFlowLayout());
p6=newJPanel(newFlowLayout());
p4.add(p1);
p4.add(p2);
tf1=newJTextField(35);
//存放显示区
tf1.setText("
0."
tf1.setEditable(false);
p5.add(tf1);
f.add(p5,BorderLayout.NORTH);
f.add(p4,BorderLayout.CENTER);
f.add(p3,BorderLayout.WEST);
b1=newJButton("
1"
b2=newJButton("
2"
b3=newJButton("
3"
b4=newJButton("
4"
b5=newJButton("
5"
b6=newJButton("
6"
b7=newJButton("
7"
b8=newJButton("
8"
b9=newJButton("
9"
b0=newJButton("
0"
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
bDiv=newJButton("
/"
bSqrt=newJButton("
sqrt"
bMulti=newJButton("
*"
bMinus=newJButton("
-"
bPercent=newJButton("
%"
bPlus=newJButton("
+"
bReciprocal=newJButton("
1/x"
bEqual=newJButton("
bDot=newJButton("
."
bNegative=newJButton("
+/-"
bDiv.addActionListener(this);
bSqrt.addActionListener(this);
bMulti.addActionListener(this);
bMinus.addActionListener(this);
bPercent.addActionListener(this);
bPlus.addActionListener(this);
bReciprocal.addActionListener(this);
bEqual.addActionListener(this);
bDot.addActionListener(this);
bNegative.addActionListener(this);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bDiv);
p2.add(bSqrt);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bMulti);
p2.add(bPercent);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMinus);
p2.add(bReciprocal);
p2.add(b0);
p2.add(bNegative);
p2.add(bDot);
p2.add(bPlus);
p2.add(bEqual);
bBackspace=newJButton("
Backspace"
bCE=newJButton("
CE"
bC=newJButton("
C"
bBackspace.addActionListener(this);
bCE.addActionListener(this);
bC.addActionListener(this);
p1.add(bBackspace);
p1.add(bCE);
p1.add(bC);
tf2=newJTextField
(2);
tf2.setEnabled(false);
tf2.setBackground(Color.LIGHT_GRAY);
bMC=newJButton("
MC"
bMR=newJButton("
MR"
bMS=newJButton("
MS"
bM=newJButton("
M+"
bMC.addActionListener(this);
bMR.addActionListener(this);
bMS.addActionListener(this);
bM.addActionListener(this);
p6.add(tf2);
p3.add(p6);
p3.add(bMC);
p3.add(bMR);
p3.add(bMS);
p3.add(bM);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听器
publicvoidactionPerformed(ActionEvente)
{
//key0to9
if(this.keyAvailable&
&
e.getActionCommand().length()==1&
e.getActionCommand().compareTo("
)>
=0&
)<
=0)
if(this.isTempNowInput)
this.dNowInput=0;
this.isTempNowInput=false;
this.nBitsNum++;
if(this.alreadyHaveDot==false)
this.dNowInput=this.dNowInput*10+Double.parseDouble(e.getActionCommand());
else
doubletemp=Double.parseDouble(e.getActionCommand());
for(inti=this.n;
i<
0;
i++)
temp*=0.1;
this.dNowInput+=temp;
this.n--;
this.tf1.setText(Double.toString(this.dNowInput));
//keydot
e.getActionCommand()=="
)
this.alreadyHaveDot=true;
this.n=-1;
//key"
"
||e.getActionCommand()=="
if(this.alreadyClickedEqueal)
this.dNowInput=this.dResult;
this.isTempNowInput=true;
switch(this.nOperation)
case1:
this.dResult+=this.dNowInput;
break;
case2:
this.dResult-=this.dNowInput;
case3:
this.dResult*=this.dNowInput;
case4:
if(this.dNowInput==0)
除数不能为零"
this.keyAvailable=false;
elsethis.dResult=this.dResult/this.dNowInput;
if(this.keyAvailable)tf1.setText(Double.toString(this.dResult));
}
if(e.getActionCommand()=="
this.nOperation=1;
this.nOperation=2;
this.nOperation=3;
this.nOperation=4;
this.nBitsNum=0;
this.alreadyClickedEqueal=false;
//key"
this.dNowInput=0-this.dNowInput;
tf1.setText(Double.toString(this.dNowInput));
this.dResult=0;
this.alreadyHaveDot=false;
this.n=0;
this.keyAvailable=true;
if(this.dResult>
this.dResult=Math.sqrt(this.dResult);
tf1.setText(Double.toString(this.dResult));
函数输入无效"
if(this.dNowInput>
this.dNowInput=Math.sqrt(this.dNowInput);
this.dNowInput=1/this.dNowInput;
this.alreadyClickedEqueal=true;
this.dMemory=this.dNowInput;
if(this.dMemory!
tf2.setText("
M"
this.dMemory=0;
"
this.dNowInput=this.dMemory;
this.dMemory+=this.dNowInput;
elsetf2.setText("
this.dNowInput=(this.dResult*this.dNowInput)/100;
if(!
this.alreadyClickedEqueal){
if(this.dNowInput!
if(this.alreadyHaveDot)
if(this.n==-1)
Stringstr,str1;
str=tf1.getText();
str1=str.substring(0,this.nBitsNum-1);
this.nBitsNum--;
this.n++;
this.dNowInput=Double.parseDouble(str1);
inttemp;
temp=(int)(this.dNowInput/10);
this.dNowInput=(double)temp;
publicstaticvoidmain(Stringargs[])
Calculationcal=newCalculation();
cal.display();
}五、运行调试与分析讨论
1.简单的运算:
(以除法为例:
569
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 计算器