java编写的简单的计算器程序文件.docx

上传人:b****5 文档编号:8837394 上传时间:2023-05-15 格式:DOCX 页数:18 大小:21.48KB
下载 相关 举报
java编写的简单的计算器程序文件.docx_第1页
第1页 / 共18页
java编写的简单的计算器程序文件.docx_第2页
第2页 / 共18页
java编写的简单的计算器程序文件.docx_第3页
第3页 / 共18页
java编写的简单的计算器程序文件.docx_第4页
第4页 / 共18页
java编写的简单的计算器程序文件.docx_第5页
第5页 / 共18页
java编写的简单的计算器程序文件.docx_第6页
第6页 / 共18页
java编写的简单的计算器程序文件.docx_第7页
第7页 / 共18页
java编写的简单的计算器程序文件.docx_第8页
第8页 / 共18页
java编写的简单的计算器程序文件.docx_第9页
第9页 / 共18页
java编写的简单的计算器程序文件.docx_第10页
第10页 / 共18页
java编写的简单的计算器程序文件.docx_第11页
第11页 / 共18页
java编写的简单的计算器程序文件.docx_第12页
第12页 / 共18页
java编写的简单的计算器程序文件.docx_第13页
第13页 / 共18页
java编写的简单的计算器程序文件.docx_第14页
第14页 / 共18页
java编写的简单的计算器程序文件.docx_第15页
第15页 / 共18页
java编写的简单的计算器程序文件.docx_第16页
第16页 / 共18页
java编写的简单的计算器程序文件.docx_第17页
第17页 / 共18页
java编写的简单的计算器程序文件.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java编写的简单的计算器程序文件.docx

《java编写的简单的计算器程序文件.docx》由会员分享,可在线阅读,更多相关《java编写的简单的计算器程序文件.docx(18页珍藏版)》请在冰点文库上搜索。

java编写的简单的计算器程序文件.docx

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

             

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2