Java计算器程序代码

用NetBeans创建窗体,再通过编写代码实现按钮的功能。步骤如下,因为是初学者,所以只实现了基本功能。

1)先创建计算机窗体

Java计算器程序代码_第1张图片

 

2为各个按钮添加事件

private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

//调用setNum方法

        setNum("1");

    }                                      

 

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

         setNum("2");

    }                                      

 

    private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

            setNum("3");

    }                                      

 

    private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("4");

    }                                      

 

    private void jButton5MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("5");

    }                                      

 

    private void jButton6MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("6");

    }                                      

 

    private void jButton7MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("7");

    }                                      

 

    private void jButton8MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("8");

    }                                      

 

    private void jButton9MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          setNum("9");

    }                                      

 

    private void jButton18MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

//调用setYSF方法

          setYSF("+");

    }                                       

 

    private void jButton19MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

          setYSF("*");

    }                                       

 

    private void jButton12MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

         setYSF("/");

    }                                       

 

    private void jButton11MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

              setYSF("-");

    }                                       

 

    private void jButton15MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code her

        

              setNum(".");

              

    }                                       

 

    private void jButton13MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

          setNum("0");

    }          

3)实现各个按钮的功能

//等于按钮的功能实现

private void dengyuMouseReleased(java.awt.event.MouseEvent evt) {                                     

       

            ScriptEngineManager manager = new ScriptEngineManager();

            ScriptEngine se = manager.getEngineByName("js");

            Double result = null;

        try {

            result = (Double) se.eval("(" + jTextField1.getText() + ")*1.0");

        } catch (ScriptException ex) {

            Logger.getLogger(JFrame.class.getName()).log(Level.SEVERE, null, ex);

             jTextField1.setText("输入错误,按c重新输入");

        }

            jTextField1.setText(result.toString());

       

        

    }                                    

//退格按钮的功能实现

    private void tuigeMouseReleased(java.awt.event.MouseEvent evt) {                                    

        //得到jTextField1中的文本

        String strText = jTextField1.getText();

//判断文本的长度是否大于1

        if(strText.length() > 1){

//如果大于1删除最后一位

          StringBuffer strBuf = new StringBuffer(strText);

          strBuf.deleteCharAt(strBuf.length() - 1);

//把文本放回jTextField1

           jTextField1.setText(strBuf.toString());

        }else{

            jTextField1.setText("0");

        }

    }                                  

    private void jButton16MouseReleased(java.awt.event.MouseEvent evt) {                                        

        // TODO add your handling code here:

          jTextField1.setText("0");

    }     

//对数字进行处理                                  

         public void setNum(String str){

        String strText = jTextField1.getText();、

//如果文本为0那么显示按键的字符

        if(strText.equals("0")){

            jTextField1.setText(str);

        }else{

//如果不是,就追加

            StringBuffer strBuf = new StringBuffer(strText);

            strBuf.append(str);

            jTextField1.setText(strBuf.toString());

        }

         }

      //对运算符进行处理              

         public void setYSF(String str){

             String strText = jTextField1.getText();

//如果最后一位为运算符,那么换为按键字符

             int lastRight = strText.length()-1;

             char ch = strText.charAt(lastRight);

             if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){

                 String strResult = strText.substring(0, lastRight)+str;

                 jTextField1.setText(strResult);

//如果不是,那么可以继续加入按键字符

             }else{

                 jTextField1.setText(strText+str);

             }

         }   

4)运行文件

Java计算器程序代码_第2张图片

 

         结果显示

Java计算器程序代码_第3张图片

 

你可能感兴趣的:(Java)