用JAVA编写计算器程序模拟Windows计算器Word格式.docx

上传人:b****6 文档编号:17525511 上传时间:2022-12-07 格式:DOCX 页数:14 大小:16.63KB
下载 相关 举报
用JAVA编写计算器程序模拟Windows计算器Word格式.docx_第1页
第1页 / 共14页
用JAVA编写计算器程序模拟Windows计算器Word格式.docx_第2页
第2页 / 共14页
用JAVA编写计算器程序模拟Windows计算器Word格式.docx_第3页
第3页 / 共14页
用JAVA编写计算器程序模拟Windows计算器Word格式.docx_第4页
第4页 / 共14页
用JAVA编写计算器程序模拟Windows计算器Word格式.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

用JAVA编写计算器程序模拟Windows计算器Word格式.docx

《用JAVA编写计算器程序模拟Windows计算器Word格式.docx》由会员分享,可在线阅读,更多相关《用JAVA编写计算器程序模拟Windows计算器Word格式.docx(14页珍藏版)》请在冰豆网上搜索。

用JAVA编写计算器程序模拟Windows计算器Word格式.docx

TextFieldtf1,tf2;

Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9,b0;

ButtonbDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative;

ButtonbBackspace,bCE,bC,bMR,bMS,bMC,bM;

publicvoiddisplay()

f=newFrame("

计算器"

);

f.setSize(280,213);

f.setLocation(200,200);

f.setBackground(Color.LIGHT_GRAY);

f.setResizable(false);

f.setLayout(newBorderLayout(3,3));

p1=newPanel(newGridLayout(1,3,5,5));

//用于存放backspace,ce,c三键

p2=newPanel(newGridLayout(4,5,5,5));

//用于存放数字区及附近共20键,此处间隙设置可能不合理,以后调整

p3=newPanel(newGridLayout(5,1,5,5));

//用于存放MC,MR,MS,M+键及显示M状态文本框,此处间隙设置可能不合理,以后调整

p4=newPanel(newFlowLayout());

//用于存放p1,p2

p5=newPanel(newFlowLayout());

p6=newPanel(newFlowLayout());

p4.add(p1);

p4.add(p2);

tf1=newTextField(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=newButton("

1"

b2=newButton("

2"

b3=newButton("

3"

b4=newButton("

4"

b5=newButton("

5"

b6=newButton("

6"

b7=newButton("

7"

b8=newButton("

8"

b9=newButton("

9"

b0=newButton("

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=newButton("

/"

bSqrt=newButton("

sqrt"

bMulti=newButton("

*"

bMinus=newButton("

-"

bPercent=newButton("

%"

bPlus=newButton("

+"

bReciprocal=newButton("

1/x"

bEqual=newButton("

bDot=newButton("

."

bNegative=newButton("

+/-"

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=newButton("

Backspace"

bCE=newButton("

CE"

bC=newButton("

C"

bBackspace.addActionListener(this);

bCE.addActionListener(this);

bC.addActionListener(this);

p1.add(bBackspace);

p1.add(bCE);

p1.add(bC);

tf2=newTextField

(2);

tf2.setEnabled(false);

tf2.setBackground(Color.LIGHT_GRAY);

bMC=newButton("

MC"

bMR=newButton("

MR"

bMS=newButton("

MS"

bM=newButton("

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.addWindowListener(this);

}

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.nBitsNum++;

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));

this.dNowInput=0;

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();

publicvoidwindowClosing(WindowEvente)

System.exit(0);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1