Java计算器的实现说明

刘恒2015257770282

一、    功能:

1.可以进行正常加减乘除运算

2.可以进行其他科学运算,我这里设置了sin正弦运算(要先输入运算数,再按sin)

其他运算实现也很容易。

3,可以进行文件读取和保存工作

二、    测试图片

界面如下:

加法测试:

先按clear清零

输入1+1=

科学计算测试:

先按clear清零

输入90  再按sin

文件存储测试:

textarea里面是这个

点击File-save 存为test.txt

文件打开测试

设置test为这个

点击file-open  打开test


附完整代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
 
// @author 刘恒2014357770282
 
publicclassCalculator  {
   MenuItemOPEN=newMenuItem("open");
   MenuItemSAVE=newMenuItem("save");
//新建窗体
   JFrameframe;
   JButtonbuttonzero,buttondot,buttonequal;//按钮“0”“.”“=”
   JButtonbuttonplus,buttonminus,buttonmultiple,buttondevision,
            buttonsin,buttontozero;//按钮“+”“-”“*”“/”“sin”和归零按钮
   JButtonbuttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix,
            buttonseven,buttoneight,buttonnine;//数字按钮“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”
   JPanelpanelwest,panelcenter,paneleast;//三个面板
   
   TextAreatf;//新建文本区域
   publicCalculator(){
        tf=newTextArea(5,5);//构造TextArea
        frame=newJFrame("Calculator");//构造窗体对象,名称为“Calculator”
        panelcenter=newJPanel();//放到窗体中央
        panelwest=newJPanel();//放到窗体西边
        paneleast=newJPanel();//放到窗体东边
        
        Handleh=newHandle();//Handle类为事件监听类
        Menufile=newMenu("File");//构造菜单
        file.add(OPEN);OPEN.setEnabled(true);OPEN.addActionListener(h);//菜单监听
      file.add(SAVE);SAVE.setEnabled(true);SAVE.addActionListener(h);//菜单监听
      MenuBarbar=newMenuBar();
      bar.add(file);
      frame.setMenuBar(bar);//设置菜单格式
      frame.setSize(100,100);
      frame.setVisible(true);
        //创建数字按钮对象,1、2、3、4、5、6、7、8、9
        buttonone=newJButton("1");
        buttontwo=newJButton("2");
        buttonthree=newJButton("3");
        buttonfour=newJButton("4");
        buttonfive=newJButton("5");
        buttonsix=newJButton("6");
        buttonseven=newJButton("7");
        buttoneight=newJButton("8");
        buttonnine=newJButton("9");
        panelcenter.setLayout(newGridLayout(3,3));//设置面板布局为网格布局,3行3列
        //将数字按钮添加到中间面板
        panelcenter.add(buttonone);
        panelcenter.add(buttontwo);
        panelcenter.add(buttonthree);
        panelcenter.add(buttonfour);
        panelcenter.add(buttonfive);
        panelcenter.add(buttonsix);
        panelcenter.add(buttonseven);
        panelcenter.add(buttoneight);
        panelcenter.add(buttonnine);
        //为10个按钮注册事件监听器
        buttonone.addActionListener(h);
        buttontwo.addActionListener(h);
        buttonthree.addActionListener(h);
        buttonfour.addActionListener(h);
        buttonfive.addActionListener(h);
        buttonsix.addActionListener(h);
        buttonseven.addActionListener(h);
        buttoneight.addActionListener(h);
        buttonnine.addActionListener(h);
        //构造按钮“0”“.”“=”,注册事件监听器,设置1行3列的布局,添加到到西边的面板
        buttonzero=newJButton("0");
        buttondot=newJButton(".");
        buttonequal=newJButton("=");
        buttonzero.addActionListener(h);
        buttondot.addActionListener(h);
        buttonequal.addActionListener(h);
        panelwest.setLayout(newGridLayout(3,1));
        panelwest.add(buttonzero);
        panelwest.add(buttondot);
        panelwest.add(buttonequal);      
        //构造操作按钮“+”“-”“*”“/”“sin”“Clear”,其中“Clear”为归零按钮
        buttonplus=newJButton("+");
        buttonminus=newJButton("-");
        buttonmultiple=newJButton("*");
        buttondevision=newJButton("/");
        buttonsin=newJButton("sin");
        buttontozero=newJButton("Clear");
        paneleast.setLayout(newGridLayout(2,1));//设置西边的布局为2行1列
        //将操作按钮“+”“-”“*”“/”“sin”“Clear”添加到西边的面板中
        paneleast.add(buttonplus);
        paneleast.add(buttonminus);
        paneleast.add(buttonmultiple);
        paneleast.add(buttondevision);
        paneleast.add(buttonsin);
        paneleast.add(buttontozero);
        //为操作按钮“+”“-”“*”“/”“sin”“Clear”注册监听器
        buttonplus.addActionListener(h);
        buttonminus.addActionListener(h);
        buttonmultiple.addActionListener(h);
        buttondevision.addActionListener(h);
        buttonsin.addActionListener(h);
        buttontozero.addActionListener(h);
         
 
        frame.setLayout(newBorderLayout());//设置窗体为边界布局
        frame.add(paneleast,"East");//将东边面板paneleast添加到窗体的东边
        frame.add(tf,BorderLayout.NORTH);//将tf文本区域添加到窗体的北边,即顶部
        frame.add(panelwest,BorderLayout.WEST);//将panelwest面板添加到窗体西边
        frame.add(panelcenter,BorderLayout.CENTER);//将panelcenter面板添加到窗体中间
        frame.pack();//设置窗体大小,适合其子组件的首选大小和布局
        frame.setLocation(500,500);//设置窗体显示位置为(500,500)
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置布局窗体默认关闭方式
        frame.setVisible(true);//设置窗体可见
   }
   publicstaticvoidmain(String[]args){
        newCalculator();//新建计算器
   }
   publicvoidreadFile(Stringfile){
      //读文件
      tf.setText("");
      try{
         BufferedReaderin=newBufferedReader(newFileReader(file));//构造文件流
         Stringline;
         while((line=in.readLine())!=null)//读文件
            tf.append(line+"\n");//追加到文本后面
         in.close();
         tf.setCaretPosition(0);
      }catch(IOExceptionioe){
         System.err.println(ioe);
      }
      
   }
   classHandleimplementsActionListener{//实现动作监听器类
        intbiaozhi=0;//此标志标志加减乘除操作
        doubleflag1=0,flag2=0,flag3=0;//flag1、flag2为两个操作数,flag3为结果
        @Override
        publicvoidactionPerformed(ActionEvente){  //方法重写  
            try{
                if(e.getSource()==buttondot){//小数点
                    tf.setText("0.");
                }
                 if(e.getSource()==buttontozero){//归零操作
                tf.setText("");
            }
                  if(e.getSource()==buttonzero){//按键0操作
                tf.setText(tf.getText()+"0");
                flag1=Double.parseDouble(tf.getText());//将文本区域转换成Double类型,赋给flag1
            }
                    if(e.getSource()==buttonone){//按键1操作
                tf.setText(tf.getText()+"1");
                flag1=Double.parseDouble(tf.getText());
            }
                      elseif(e.getSource()==buttontwo){//按键2操作
                tf.setText(tf.getText()+"2");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttonthree){//按键3操作
                tf.setText(tf.getText()+"3");
                flag1=Double.parseDouble(tf.getText());
            }
                       elseif(e.getSource()==buttonfour){//按键4操作
                tf.setText(tf.getText()+"4");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttonfive){//按键5操作
                tf.setText(tf.getText()+"5");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttonsix){//按键6操作
                tf.setText(tf.getText()+"6");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttonseven){//按键7操作
                tf.setText(tf.getText()+"7");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttoneight){//按键8操作
                tf.setText(tf.getText()+"8");
                flag1=Double.parseDouble(tf.getText());
            }
            elseif(e.getSource()==buttonnine){//按键9操作
                tf.setText(tf.getText()+"9");
                flag1=Double.parseDouble(tf.getText());
            }
            if(e.getSource()==buttonplus){//加法操作
                tf.setText("");
                flag2=flag1;
                biaozhi=0;
            }
            if(e.getSource()==buttonminus){//减法操作
                tf.setText("");
                flag2=flag1;
                biaozhi=1;
            }
               if(e.getSource()==buttonmultiple){//乘法操作
                tf.setText("");
                flag2=flag1;
                biaozhi=2;
            }
            if(e.getSource()==buttondevision){//除法操作
                tf.setText("");
                flag2=flag1;
                biaozhi=3;
            }
            if(e.getSource()==buttonsin){//正弦操作
                flag3=Math.sin(Math.PI*flag1/180);
                tf.setText(flag3+"");
            }
                  
            if(e.getSource()==buttonequal){//等号操作,利用判断进行相应加减乘除操作
                if(biaozhi==0){
                    flag3=flag1+flag2;
                }
                if(biaozhi==1){
                    flag3=flag1-flag2;
                }
                if(biaozhi==2){
                    flag3=flag1*flag2;
                }
                if(biaozhi==3){
                    flag3=flag1/flag2;
                }
                tf.setText("Result="+flag3+"");
            }
            if(e.getSource()==OPEN)
         {
            FileDialogfd=newFileDialog(frame,"open file",FileDialog.LOAD);//FileDialog窗口使用
            fd.setVisible(true);
            if(fd.getFile()!=null){
               Filefile=newFile(fd.getDirectory()+fd.getFile());
               if(file.exists())
                  readFile(file.toString());
                  else
                     tf.setText("File name:"+file+"invalid");
            }
            fd.dispose();
         }
 
 
         if(e.getSource()==SAVE){
            FileDialogfd=newFileDialog(frame,"save file",FileDialog.SAVE);
            fd.setVisible(true);
            try{
               Stringtxt=tf.getText();
               FileOutputStreamout=newFileOutputStream(fd.getDirectory()+fd.getFile()+".txt");
               out.write(txt.getBytes());           
                  out.close();
            }catch(FileNotFoundExceptione1){//存为.txt格式,文件名为你输入的字符串
         }catch(IOExceptione1){
               // TODO Auto-generated catch block
               e1.printStackTrace();
            }
      }
            }catch(Exceptionex){
            }
        }
        }
}