java编写的简单的计算器程序文件.docx
《java编写的简单的计算器程序文件.docx》由会员分享,可在线阅读,更多相关《java编写的简单的计算器程序文件.docx(18页珍藏版)》请在冰点文库上搜索。
java编写的简单的计算器程序文件
计算器
项目容:
编写一个Applet,模仿windows附件所带计算器的功能,可以帮助用户完成计算功能,具体如下图所示。
项目要求:
使用图形的方式借助窗口、菜单、按钮等标准界面元素和鼠标操作,来帮助用户方便地向计算机系统发出命令,启动操作,并将系统运行的结果同样以图形的方式显示给用户,这样更加直观和生动;
1.Applet容器中组件的添加与设置,包括面板以及菜单的使用;
2.容器中组件的布局管理;
3.Java核心包中数组、数学计算类的使用;
4.异常的处理;
5.事件处理模型中的三类对象的使用:
1.Event-事件,用户对界面操作在java语言上的描述,以类的形式出现,例如键盘操作对应的事件类是KeyEvent。
2.EventSource-事件源,事件发生的场所,通常就是各个组件,例如按钮Button。
3.Eventhandler-事件处理者,接收事件对象并对其进行处理的对象。
6.程序中事件处理的流程:
1.计算流程的细化
参考代码:
importjava.awt.*;
importjava.lang.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
publicclassCalculator
implementsActionListener{//导入动作监听接口
//设计面板中的单位
JFrameframe;
JTextFieldtextAnswer;
JPanelpanel,panel1,panel2,panel3;
JMenuBarmainMenu;
JTextFieldtextMemory;
JLabellabelMemSpace;//labelMemSpace单纯做摆设,控制面板的形状
JButtonbuttonBk,buttonCe,buttonC;
JButtonbutton[];
JButtonbuttonMC,buttonMR,buttonMS,buttonMAdd;
JButtonbuttonDot,buttonAddAndSub,buttonAdd,buttonSub,buttonMul,
buttonDiv,buttonMod;
JButtonbuttonSqrt,buttonDao,buttonEqual;
JMenueditMenu,viewMenu,helpMenu;
JMenuItemcopyItem,pasteItem,tItem,sItem,numberGroup,topHelp,aboutCal;
DecimalFormatdf;//设置数据输出精度
booleanclickable;//控制当前能否按键
doublememoryd;//使用存中存储的数字
intmemoryi;
doublevard,answerd;//用来保存double型数据的中间值(vard)和最后结果(answerd)
shortkey=-1,prekey=-1;//key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
Stringcopy;//做复制用
JTextAreahelp;//帮助
JScrollPanescrollHelp;
//构造函数
publicCalculator(){
clickable=true;
answerd=0;
frame=newJFrame("计算器");
df=newDecimalFormat("0.##############");//设置数据输出精度(对于double型值)
textAnswer=newJTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(newColor(255,255,255));
panel=newJPanel();
frame.getContentPane().add(panel);
panel1=newJPanel();
panel2=newJPanel();
panel.setLayout(newBorderLayout());
//设计整个面板
mainMenu=newJMenuBar();
editMenu=newJMenu("编辑(E)");
viewMenu=newJMenu("查看(V)");
helpMenu=newJMenu("帮助(H)");
copyItem=newJMenuItem(" 复制(C)Ctrl+C");
copyItem.addActionListener(this);
pasteItem=newJMenuItem(" 粘贴(V)Ctrl+V");
pasteItem.addActionListener(this);
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem=newJMenuItem("●标准型(T)");
tItem.addActionListener(this);
sItem=newJMenuItem(" 科学型(S)");
sItem.addActionListener(this);
numberGroup=newJMenuItem(" 数字分组(I)");
numberGroup.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);
viewMenu.add(numberGroup);
topHelp=newJMenuItem(" 帮助主题(H)");
topHelp.addActionListener(this);
help=newJTextArea(5,20);
scrollHelp=newJScrollPane(help);
help.setEditable(false);
help.append("执行简单计算\n");
help.append("1. 键入计算的第一个数字。
\n");
help.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。
\n");
help.append("3. 键入计算的下一个数字。
\n");
help.append("4. 输入所有剩余的运算符和数字。
\n");
help.append("5. 单击“=”。
\n");
aboutCal=newJMenuItem(" 关于计算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
panel.add(mainMenu,BorderLayout.NORTH);
panel.add(textAnswer,BorderLayout.CENTER);
panel.add(panel1,BorderLayout.SOUTH);
panel1.setLayout(newBorderLayout());
textMemory=newJTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(newColor(217,217,217));
labelMemSpace=newJLabel(" ");
buttonBk=newJButton("Backspace");
buttonBk.setForeground(newColor(255,0,0));
buttonCe=newJButton("CE");
buttonCe.setForeground(newColor(255,0,0));
buttonC=newJButton("C");
buttonC.setForeground(newColor(255,0,0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2,BorderLayout.NORTH);
panel2.setLayout(newFlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel3=newJPanel();
panel1.add(panel3,BorderLayout.CENTER);
button=newJButton[10];
for(inti=0;i button[i]=newJButton(Integer.toString(i));
button[i].setForeground(newColor(0,0,255));
}
buttonMC=newJButton("MC");
buttonMC.setForeground(newColor(255,0,0));
buttonMR=newJButton("MR");
buttonMR.setForeground(newColor(255,0,0));
buttonMS=newJButton("MS");
buttonMS.setForeground(newColor(255,0,0));
buttonMAdd=newJButton("M+");
buttonMAdd.setForeground(newColor(255,0,0));
buttonDot=newJButton(".");
buttonDot.setForeground(newColor(0,0,255));
buttonAddAndSub=newJButton("+/-");
buttonAddAndSub.setForeground(newColor(0,0,255));
buttonAdd=newJButton("+");
buttonAdd.setForeground(newColor(255,0,0));
buttonSub=newJButton("-");
buttonSub.setForeground(newColor(255,0,0));
buttonMul=newJButton("*");
buttonMul.setForeground(newColor(255,0,0));
buttonDiv=newJButton("/");
buttonDiv.setForeground(newColor(255,0,0));
buttonMod=newJButton("%");
buttonMod.setForeground(newColor(0,0,255));
buttonSqrt=newJButton("sqrt");
buttonSqrt.setForeground(newColor(0,0,255));
buttonDao=newJButton("1/x");
buttonDao.setForeground(newColor(0,0,255));
buttonEqual=newJButton("=");
buttonEqual.setForeground(newColor(255,0,0));
//将所有行为与监听绑定
panel3.setLayout(newGridLayout(4,6));
panel3.add(buttonMC);
buttonMC.addActionListener(this);
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(buttonSqrt);
buttonSqrt.addActionListener(this);
panel3.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(buttonMod);
buttonMod.addActionListener(this);
panel3.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(buttonDao);
buttonDao.addActionListener(this);
panel3.add(buttonMAdd);
buttonMAdd.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonAddAndSub);
buttonAddAndSub.addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
//设置各个按钮行为
publicvoidactionPerformed(ActionEventevent){
booleansign=false;//判断是否是double型数参与运算,是为true,不是为false
Objecttemp=event.getSource();
try{
//如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后
for(inti=0;i<=9;i++)
if(temp==button[i]&&clickable==true)
textAnswer.setText(textAnswer.getText()+Integer.toString(i));
//按下'.'按钮时,判断当前文本框字符串中含不含'.',如果已含,则不允许再插入'.'
if(temp==buttonDot&&clickable==true){
booleanisDot=false;
if(textAnswer.getText().length()==0)
isDot=true;
for(inti=0;i if('.'==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
if(isDot==false)
textAnswer.setText(textAnswer.getText()+".");
}
if((temp==buttonAdd||temp==buttonSub||temp==buttonMul||
temp==buttonDiv)&&clickable==true){
//'+'操作
if(temp==buttonAdd){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());
break;
case2:
answerd*=Double.parseDouble(textAnswer.getText());
break;
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不能为零");
clickable=false;
}
else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=0;
}
//'-'操作
if(temp==buttonSub){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());