java程序设计案例2计算器.docx
- 文档编号:23671621
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:23
- 大小:35.35KB
java程序设计案例2计算器.docx
《java程序设计案例2计算器.docx》由会员分享,可在线阅读,更多相关《java程序设计案例2计算器.docx(23页珍藏版)》请在冰豆网上搜索。
java程序设计案例2计算器
案例剖析
案例二:
计算器
所属章节:
JAVA的控件
教学目的:
∙掌握Applet的基本编写
∙掌握字体和颜色的设置
∙掌握容器的使用
∙掌握布局管理器的使用
∙菜单类组件的使用
设计要求:
∙设计一个标准型的计算机程序
∙该程序可以通过鼠标依次输入参加计算的数值,不仅可以参加加、减、乘、除等简单的运算,还可以完成sqrt、%、1/x等运算.
相关知识
∙图形绘制
∙Color类、Math类
∙日期时间的格式化
案例剖析
∙该计算器程序由一个JAVA源文件组成,即Claculator.java.
∙Claculator.java中有3个方法:
Claculator(构造方法)、actionPerformed(处理事件方法)、main(主方法)
∙main(主方法)是程序的入口
∙在Claculator方法中主要完成对Claculator类对象的初始代操作,在actionPerformed方汉中实现各个按钮的功能.
参考代码
//计算器
importjava.awt.*;
importjava.lang.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
publicclassCalculatorimplementsActionListener{//导入动作监听接口
//设计面板中的单位
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()); 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=1; } //'*'操作 if(temp==buttonMul){ 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=2; } //'/'操作 if(temp==buttonDiv){ switch(prekey){ case0: answerd+=Double.parseDouble(textAnswer.getText());
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 程序设计 案例 计算器