计算器的实现

package javaapplication3;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 *
 * @author Administrator
 */
public class NewJFrame extends javax.swing.JFrame {
    private Object strTest1;
    private Object strText;

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jButton18 = new javax.swing.JButton();
        jButton20 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setForeground(java.awt.Color.white);

        jPanel2.setOpaque(false);

        jTextField1.setFont(new java.awt.Font("宋体", 0, 36)); // NOI18N
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextField1.setText("0");

        jButton1.setBackground(new java.awt.Color(102, 102, 255));
        jButton1.setText("7");
        jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });

        jButton2.setBackground(new java.awt.Color(204, 102, 255));
        jButton2.setText("8");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton2MouseReleased(evt);
            }
        });

        jButton3.setBackground(new java.awt.Color(153, 0, 102));
        jButton3.setText("C");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton3MouseReleased(evt);
            }
        });

        jButton4.setBackground(new java.awt.Color(0, 153, 204));
        jButton4.setText("9");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton4MouseReleased(evt);
            }
        });

        jButton5.setBackground(new java.awt.Color(102, 102, 0));
        jButton5.setText("4");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton5MouseReleased(evt);
            }
        });

        jButton6.setBackground(new java.awt.Color(51, 255, 51));
        jButton6.setText("5");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton6MouseReleased(evt);
            }
        });

        jButton7.setBackground(new java.awt.Color(0, 255, 255));
        jButton7.setText("6");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton7MouseReleased(evt);
            }
        });

        jButton9.setBackground(new java.awt.Color(255, 0, 51));
        jButton9.setText("1");
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton9MouseReleased(evt);
            }
        });

        jButton10.setBackground(new java.awt.Color(255, 102, 0));
        jButton10.setText("2");
        jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton10MouseReleased(evt);
            }
        });

        jButton11.setBackground(new java.awt.Color(255, 255, 51));
        jButton11.setText("3");
        jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton11MouseReleased(evt);
            }
        });
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setBackground(new java.awt.Color(0, 255, 102));
        jButton12.setText("←");
        jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton12MouseReleased(evt);
            }
        });

        jButton13.setBackground(new java.awt.Color(153, 153, 255));
        jButton13.setText("0");
        jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton13MouseReleased(evt);
            }
        });

        jButton14.setBackground(new java.awt.Color(204, 0, 204));
        jButton14.setText("=");
        jButton14.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton14MouseReleased(evt);
            }
        });

        jButton15.setBackground(new java.awt.Color(102, 102, 255));
        jButton15.setText(".");
        jButton15.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton15MouseReleased(evt);
            }
        });

        jButton16.setBackground(new java.awt.Color(153, 204, 0));
        jButton16.setText("/");
        jButton16.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton16MouseReleased(evt);
            }
        });

        jButton17.setBackground(new java.awt.Color(0, 102, 0));
        jButton17.setText("*");
        jButton17.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton17MouseReleased(evt);
            }
        });

        jButton18.setBackground(new java.awt.Color(51, 255, 51));
        jButton18.setText("-");
        jButton18.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton18MouseReleased(evt);
            }
        });

        jButton20.setBackground(new java.awt.Color(102, 51, 255));
        jButton20.setText("+");
        jButton20.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton20MouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 381, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton20, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jButton18, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
                                .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(67, Short.MAX_VALUE)))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton10, jButton11, jButton12, jButton15, jButton16, jButton17, jButton18, jButton2, jButton20, jButton3, jButton4, jButton5, jButton6, jButton7, jButton9});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 340, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton12)
                            .addComponent(jButton16)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton18)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton15)
                                .addComponent(jButton13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(100, Short.MAX_VALUE)))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton10, jButton11, jButton12, jButton15, jButton16, jButton17, jButton18, jButton2, jButton20, jButton3, jButton4, jButton5, jButton6, jButton7, jButton9});

        jPanel1.setOpaque(false);

        jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\Jisuanqi\\QQ截图20150917105823.png")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 62, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 63, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
        jLayeredPane1.setLayout(jLayeredPane1Layout);
        jLayeredPane1Layout.setHorizontalGroup(
            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jLayeredPane1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jLayeredPane1Layout.setVerticalGroup(
            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jLayeredPane1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jLayeredPane1.setLayer(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jLayeredPane1.setLayer(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                       

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void jButton9MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("1");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("1");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton10MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("2");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("2");
             jTextField1.setText(strBuf.toString());
         }
    }                                      

    private void jButton11MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("3");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("3");
             jTextField1.setText(strBuf.toString());
         }
    }                                      

    private void jButton5MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("4");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("4");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton6MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("5");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("5");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton7MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("6");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("6");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("7");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("7");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("8");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("8");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("9");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("9");
             jTextField1.setText(strBuf.toString());
         }
    }                                     

    private void jButton13MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
        String strText1=new String(jTextField1.getText());
        if (strText1.equals("0")){
             jTextField1.setText("0");
         }else{
             StringBuffer strBuf=new StringBuffer(strText1);
             strBuf.append("0");
             jTextField1.setText(strBuf.toString());
         }
    }                                      

    private void jButton20MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
     setYSF("+");
    }                                      
           public void setYSF(String str){
    //取出当前显示的内容放到字符串变量strTet中
    String strText=jTextField1.getText();
    //判断strText中字符串的最右边一位(最后一个值)是否为运算符
    int lastRight=strText.length()-1;
    char ch=strText.charAt(lastRight);
    if(ch=='='||ch=='-'||ch=='*'||ch=='/'||ch=='.'){
        //替换strText最右边的运算符为当前按键str 比如”12345““1234”
        String strResult=strText.substring(0,lastRight)+str;
        //strText.setResult放回文本框
        jTextField1.setText(strResult);
    }else{
        //strText追加当前st
   
    jTextField1.setText(strText+str);
    //strText放回
            }
}
       
    private void jButton18MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
       setYSF("-");
    }                                      

    private void jButton17MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
       setYSF("*");
    }                                      

    private void jButton16MouseReleased(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:
        String str=jTextField1.getText();
        StringBuffer strBuff=new StringBuffer(str);
        if(str.length()<2){jTextField1.setText("0");}
        else{
            strBuff.deleteCharAt(str.length()-1);
            jTextField1.setText(strBuff.toString());
        }
    
    }                                      

    private void jButton15MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
         setYSF(".");
    
       
          
    }                                      

    private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        jTextField1.setText("0");
       }                                      
 public void setText(String str){
    String strText=jTextField1.getText();
    if (strText.equals("0")){
     jTextField1.setText(str);
    }else{
     
     jTextField1.setText(strText+str) ; 
    }
    }                                     

    private void jButton14MouseReleased(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
          ScriptEngineManager  manager=new   ScriptEngineManager();
       ScriptEngine se=manager.getEngineByName("js");
         Double result=null;
        try {
            result=(Double)se.eval("("+jTextField1.getText()+")*1.0");// TODO add your handling code here:
        } catch (ScriptException ex) {
            Logger.getLogger(JFrame1.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTextField1.setText(result.toString());
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton18;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton20;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  
}

 

实现结果输出、

*/数字按钮输出显示

private voidjButton10MouseReleased(java.awt.event.MouseEvent evt) {                                       

       // TODO add your handling code here:

       String strText1=new String(jTextField1.getText());

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

            jTextField1.setText("2");

        }else{

            StringBuffer strBuf=new StringBuffer(strText1);

            strBuf.append("2");

            jTextField1.setText(strBuf.toString());

        }

}  

        

*/操作符,等号,小数点按钮输出显示

             private voidjButton20MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

     setYSF("+");

    }                                      

           public void setYSF(String str){

    //取出当前显示的内容放到字符串变量strTet

    String strText=jTextField1.getText();

    //判断strText中字符串的最右边一位(最后一个值)是否为运算符

    int lastRight=strText.length()-1;

    char ch=strText.charAt(lastRight);

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

        //替换strText最右边的运算符为当前按键str比如”12345““1234

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

        //strText.setResult放回文本框

        jTextField1.setText(strResult);

    }else{

        //strText追加当前st

   

    jTextField1.setText(strText+str);

    //strText放回

            }

}

    private voidjButton14MouseReleased(java.awt.event.MouseEvent evt) {                                       

        // TODO add your handling code here:

          ScriptEngineManager  manager=new  ScriptEngineManager();

       ScriptEnginese=manager.getEngineByName("js");

         Double result=null;

        try {

           result=(Double)se.eval("("+jTextField1.getText()+")*1.0");//TODO add your handling code here:

        } catch (ScriptException ex) {

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

        }

        jTextField1.setText(result.toString());

    }                               

   

   

   

*/清零按钮输出

  private void jButton3MouseReleased(java.awt.event.MouseEventevt) {                                      

        // TODO add your handling code here:

        jTextField1.setText("0");

       }                                      

 public void setText(String str){

    String strText=jTextField1.getText();

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

     jTextField1.setText(str);

    }else{

     

     jTextField1.setText(strText+str) ; 

    }

    }    

  

*/退格按钮输出

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

       // TODO add your handling code here:

       String str=jTextField1.getText();

       StringBuffer strBuff=new StringBuffer(str);

       if(str.length()<2){jTextField1.setText("0");}

       else{

           strBuff.deleteCharAt(str.length()-1);

           jTextField1.setText(strBuff.toString());

       }

    

}  

       

           

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值