您的位置:首页 > 其它

简易计算器

2015-09-20 20:36 225 查看
/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package folder;

import static java.awt.SystemColor.text;

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 {

    

    /**

     * 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">//GEN-BEGIN:initComponents

    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();

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

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

        jPanel2 = new javax.swing.JPanel();

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setAutoRequestFocus(false);

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setOpaque(false);

        jTextField1.setFont(new java.awt.Font("宋体", 0, 36)); // NOI18N

        jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jTextField1.setText("0");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jTextField1ActionPerformed(evt);

            }

        });

        jButton1.setText("7");

        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton1MouseReleased(evt);

            }

        });

        jButton2.setText("2");

        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton2MouseReleased(evt);

            }

        });

        jButton3.setText("9");

        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton3MouseReleased(evt);

            }

        });

        jButton3.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton3ActionPerformed(evt);

            }

        });

        jButton4.setText("8");

        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton4MouseReleased(evt);

            }

        });

        jButton5.setText("6");

        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton5MouseReleased(evt);

            }

        });

        jButton6.setText("3");

        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton6MouseReleased(evt);

            }

        });

        jButton6.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton6ActionPerformed(evt);

            }

        });

        jButton7.setText("4");

        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton7MouseReleased(evt);

            }

        });

        jButton8.setText("1");

        jButton8.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton8MouseReleased(evt);

            }

        });

        jButton8.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton8ActionPerformed(evt);

            }

        });

        jButton9.setText("/");

        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton9MouseReleased(evt);

            }

        });

        jButton10.setText(".");

        jButton10.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton10MouseReleased(evt);

            }

        });

        jButton10.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton10ActionPerformed(evt);

            }

        });

        jButton11.setText("0");

        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.setText("+");

        jButton12.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton12MouseReleased(evt);

            }

        });

        jButton12.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton12ActionPerformed(evt);

            }

        });

        jButton13.setText("5");

        jButton13.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton13MouseReleased(evt);

            }

        });

        jButton14.setText("*");

        jButton14.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton14MouseReleased(evt);

            }

        });

        jButton15.setText("-");

        jButton15.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton15MouseReleased(evt);

            }

        });

        jButton16.setText("CE");

        jButton16.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton16MouseReleased(evt);

            }

        });

        jButton17.setText("←");

        jButton17.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton17MouseReleased(evt);

            }

        });

        jButton18.setText("=");

        jButton18.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jButton18MouseReleased(evt);

            }

        });

        jButton18.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton18ActionPerformed(evt);

            }

        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

        jPanel1.setLayout(jPanel1Layout);

        jPanel1Layout.setHorizontalGroup(

            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 100, Short.MAX_VALUE)

            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jPanel1Layout.createSequentialGroup()

                    .addContainerGap()

                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                        .addGroup(jPanel1Layout.createSequentialGroup()

                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

                                    .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                                    .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))

                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()

                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                                    .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addGap(10, 10, 10)))

                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

                                .addComponent(jButton18, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)

                                .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))

                        .addGroup(jPanel1Layout.createSequentialGroup()

                            .addGap(41, 41, 41)

                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE))

                        .addGroup(jPanel1Layout.createSequentialGroup()

                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                            .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))

                        .addGroup(jPanel1Layout.createSequentialGroup()

                            .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

                            .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))

                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

        );

        jPanel1Layout.setVerticalGroup(

            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 100, Short.MAX_VALUE)

            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jPanel1Layout.createSequentialGroup()

                    .addContainerGap()

                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGap(21, 21, 21)

                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))

                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                        .addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))

                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                        .addGroup(jPanel1Layout.createSequentialGroup()

                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                                .addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))

                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

                                    .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)

                                    .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))

                                .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))

                        .addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))

                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

        );

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Administrator\\Desktop\\8039699682815826838.jpg")); // NOI18N

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);

        jPanel2.setLayout(jPanel2Layout);

        jPanel2Layout.setHorizontalGroup(

            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 399, Short.MAX_VALUE)

            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()

                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

        );

        jPanel2Layout.setVerticalGroup(

            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 341, Short.MAX_VALUE)

            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jPanel2Layout.createSequentialGroup()

                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))

        );

        javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);

        jLayeredPane1.setLayout(jLayeredPane1Layout);

        jLayeredPane1Layout.setHorizontalGroup(

            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 399, Short.MAX_VALUE)

            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jLayeredPane1Layout.createSequentialGroup()

                    .addGap(0, 0, Short.MAX_VALUE)

                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGap(0, 0, Short.MAX_VALUE)))

            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jLayeredPane1Layout.createSequentialGroup()

                    .addGap(0, 0, Short.MAX_VALUE)

                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGap(0, 0, Short.MAX_VALUE)))

        );

        jLayeredPane1Layout.setVerticalGroup(

            jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 341, Short.MAX_VALUE)

            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jLayeredPane1Layout.createSequentialGroup()

                    .addGap(0, 0, Short.MAX_VALUE)

                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGap(0, 0, Short.MAX_VALUE)))

            .addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                .addGroup(jLayeredPane1Layout.createSequentialGroup()

                    .addGap(0, 0, Short.MAX_VALUE)

                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                    .addGap(0, 0, Short.MAX_VALUE)))

        );

        jLayeredPane1.setLayer(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane1.setLayer(jPanel2, 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(layout.createSequentialGroup()

                .addGap(0, 0, Short.MAX_VALUE)

                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(0, 0, Short.MAX_VALUE))

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addGap(0, 0, Short.MAX_VALUE)

                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(0, 0, Short.MAX_VALUE))

        );

        pack();

    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton11MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton11MouseReleased

        // TODO add your handling code here:

     

        setNum("0");

    }//GEN-LAST:event_jButton11MouseReleased

    private void jButton8MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton8MouseReleased

        // TODO add your handling code here:

         setNum("1");

    }//GEN-LAST:event_jButton8MouseReleased

    private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseReleased

        // TODO add your handling code here:

        setNum("2");

    }//GEN-LAST:event_jButton2MouseReleased

    private void jButton6MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton6MouseReleased

        // TODO add your handling code here:

        setNum("3");

    }//GEN-LAST:event_jButton6MouseReleased

    private void jButton7MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton7MouseReleased

        // TODO add your handling code here:

        setNum("4");

    }//GEN-LAST:event_jButton7MouseReleased

    private void jButton13MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton13MouseReleased

        // TODO add your handling code here:

        setNum("5");

    }//GEN-LAST:event_jButton13MouseReleased

    private void jButton5MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseReleased

        // TODO add your handling code here:

        setNum("6");

    }//GEN-LAST:event_jButton5MouseReleased

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased

        // TODO add your handling code here:

        setNum("7");

    }//GEN-LAST:event_jButton1MouseReleased

    private void jButton4MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseReleased

        // TODO add your handling code here:

        setNum("8");

    }//GEN-LAST:event_jButton4MouseReleased

    private void jButton3MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseReleased

        // TODO add your handling code here:

        setNum("9");

    }//GEN-LAST:event_jButton3MouseReleased

    private void jButton12MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton12MouseReleased

        // TODO add your handling code here:

        setYEF("+");

    }//GEN-LAST:event_jButton12MouseReleased

    private void jButton15MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton15MouseReleased

        // TODO add your handling code here:

        setYEF("-");

    }//GEN-LAST:event_jButton15MouseReleased

    private void jButton14MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton14MouseReleased

        // TODO add your handling code here:

        setYEF("*");

    }//GEN-LAST:event_jButton14MouseReleased

    private void jButton9MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton9MouseReleased

        // TODO add your handling code here:

        setYEF("/");

    }//GEN-LAST:event_jButton9MouseReleased

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton17MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton17MouseReleased

        // TODO add your handling code here:

        String strText = jTextField1.getText();

        if(strText.length()>1){

            StringBuffer strBuf =new StringBuffer(strText);

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

             jTextField1.setText(strBuf.toString());    

        }else{

             jTextField1.setText("0");

        }

    }//GEN-LAST:event_jButton17MouseReleased

    private void jButton16MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton16MouseReleased

        // TODO add your handling code here:

          jTextField1.setText("0");

    }//GEN-LAST:event_jButton16MouseReleased

    private void jButton18MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton18MouseReleased

        // 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");

        } catch (ScriptException ex) {

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

            jTextField1.setText("错误,按CE重输");

        }

        

        jTextField1.setText(result.toString());

    }//GEN-LAST:event_jButton18MouseReleased

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton12ActionPerformed

    private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton18ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton18ActionPerformed

    private void jButton10MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton10MouseReleased

        // TODO add your handling code here:

        String strText = jTextField1.getText();

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

            jTextField1.setText(strText + ".");

        } else {

            StringBuffer strBuf = new StringBuffer(jTextField1.getText());

//            if(strBuf.length()<2){

//                jTextField.setText(strBuf+".");

//            }else{

            int temp = 0;

            int i = strBuf.length() - 1;

            while (i > strBuf.lastIndexOf("+") && i > strBuf.lastIndexOf("-") && i > strBuf.lastIndexOf("*") && i > strBuf.lastIndexOf("/")) {

                if (strBuf.charAt(i) == '.') {

                    temp = i;

                    break;

                } else {

                    i--;

                    temp = i;

                }

                System.out.print(i);

            }

            //String str = new String(strBuf);

            if (temp == strBuf.lastIndexOf("+") || temp == strBuf.lastIndexOf("-") || temp == strBuf.lastIndexOf("*") || temp == strBuf.lastIndexOf("/") || temp == 0) {

                jTextField1.setText(strBuf.toString() + ".");

                System.out.print(strBuf.lastIndexOf("+"));

            } else {

                jTextField1.setText(strBuf.toString());

                System.out.print(strBuf.lastIndexOf("+"));

                System.out.print(temp);

            }

        }

  

    }//GEN-LAST:event_jButton10MouseReleased

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed

        // TODO add your handling code here:

    }//GEN-LAST:event_jButton10ActionPerformed

                              

    /**

     * @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//GEN-BEGIN:variables

    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 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 jButton8;

    private javax.swing.JButton jButton9;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLayeredPane jLayeredPane1;

    private javax.swing.JPanel jPanel1;

    private javax.swing.JPanel jPanel2;

    private javax.swing.JTextField jTextField1;

    // End of variables declaration//GEN-END:variables

     private void setNum(String str) {

        String strText = jTextField1.getText();

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

            jTextField1.setText(str);

        } else {

            StringBuffer strBuf = new StringBuffer(jTextField1.getText());

            if (strBuf.length() < 3) {

                //String strText2 = new String(strBuf);

                jTextField1.setText(strBuf + str);

            } else {

                char ch1 = strBuf.charAt(strBuf.length() - 1);

                char ch2 = strBuf.charAt(strBuf.length() - 2);

                if (ch1 == '0' && (ch2 == '+' || ch2 == '-' || ch2 == '*' || ch2 == '/')) {

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

                    String strText1 = new String(strBuf);

                    jTextField1.setText(strText1 + str);

                } else {

                    String strText2 = new String(strBuf);

                    jTextField1.setText(strText2 + str);

                }

            }

        }

    }

    public void setYEF(String str){

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

        String strText = jTextField1.getText();

        //判断strText中字符串的最后一位是否为运算符

        int lastRight = strText.length()-1;

        char ch = strText.charAt(lastRight);

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

        //替换最右边的运算符为当前按键str

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

        //steText放回文本框

        jTextField1.setText(strResult);

        }else{

                //strText追加当前str

        jTextField1.setText(strText+str);    

                //strText放回

            }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: