-1

我正在制作一个计算器,但我不知道该放什么 for txtDisplay 和我的 getOperator 显示它有问题,顺便说一句我第一次发帖

public class Calculater1 extends javax.swing.JFrame {
/**
 * Creates new form Calculater
 */
private double total1 = 0.0;
private double total2 = 0.0;
private char math_operator;

public Calculater1() {
    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() {

    txtDisplay = new javax.swing.JTextField();
    jPanel1 = new javax.swing.JPanel();
    BtnOne = new javax.swing.JButton();
    BtnTwo = new javax.swing.JButton();
    BtnThree = new javax.swing.JButton();
    BtnFour = new javax.swing.JButton();
    BtnFive = new javax.swing.JButton();
    BtnSix = new javax.swing.JButton();
    BtnSeven = new javax.swing.JButton();
    BtnEight = new javax.swing.JButton();
    BtnNine = new javax.swing.JButton();
    BtnZero = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    BtnAdd = new javax.swing.JButton();
    BtnSubtract = new javax.swing.JButton();
    BtnMutiply = new javax.swing.JButton();
    BtnDivide = new javax.swing.JButton();
    BtnClear = new javax.swing.JButton();
    BtnEqual = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Calculator");

    txtDisplay.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtDisplayActionPerformed(evt);
        }
    });

    BtnOne.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    BtnOne.setText("1");
    BtnOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnOneActionPerformed(evt);
        }
    });

    BtnTwo.setText("2");
    BtnTwo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnTwoActionPerformed(evt);
        }
    });

    BtnThree.setText("3");
    BtnThree.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnThreeActionPerformed(evt);
        }
    });

    BtnFour.setText("4");
    BtnFour.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnFourActionPerformed(evt);
        }
    });

    BtnFive.setText("5");
    BtnFive.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnFiveActionPerformed(evt);
        }
    });

    BtnSix.setText("6");
    BtnSix.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSixActionPerformed(evt);
        }
    });

    BtnSeven.setText("7");
    BtnSeven.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSevenActionPerformed(evt);
        }
    });

    BtnEight.setText("8");
    BtnEight.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnEightActionPerformed(evt);
        }
    });

    BtnNine.setText("9");
    BtnNine.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnNineActionPerformed(evt);
        }
    });

    BtnZero.setText("0");
    BtnZero.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnZeroActionPerformed(evt);
        }
    });

    BtnAdd.setText("+");
    BtnAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnAddActionPerformed(evt);
        }
    });

    BtnSubtract.setText("-");
    BtnSubtract.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSubtractActionPerformed(evt);
        }
    });

    BtnMutiply.setText("*");
    BtnMutiply.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnMutiplyActionPerformed(evt);
        }
    });

    BtnDivide.setText("/");
    BtnDivide.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnDivideActionPerformed(evt);
        }
    });

    BtnClear.setText("C");
    BtnClear.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnClearActionPerformed(evt);
        }
    });

    BtnEqual.setText("=");
    BtnEqual.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnEqualActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(BtnAdd)
                .addComponent(BtnSubtract)
                .addComponent(BtnMutiply)
                .addComponent(BtnDivide)
                .addComponent(BtnClear)
                .addComponent(BtnEqual))
            .addGap(0, 27, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addComponent(BtnAdd)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnSubtract)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnMutiply)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(BtnDivide)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnClear)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(BtnEqual)
            .addContainerGap(14, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BtnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(BtnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(BtnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .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))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(9, 9, 9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(BtnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addContainerGap())
    );

    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(127, 127, 127)
            .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(143, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(19, 19, 19))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(24, 24, 24)
            .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>//GEN-END:initComponents

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

}//GEN-LAST:event_txtDisplayActionPerformed

private void BtnNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNineActionPerformed
  String BtnNineText = txtDisplay.getText() + BtnNine.getText();
  txtDisplay.setText( BtnNineText );  
}//GEN-LAST:event_BtnNineActionPerformed

private void BtnEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnEightActionPerformed
    String BtnEightText = txtDisplay.getText() + BtnEight.getText();
    txtDisplay.setText( BtnEightText );
}//GEN-LAST:event_BtnEightActionPerformed

private void BtnOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnOneActionPerformed
    String BtnOneText = txtDisplay.getText() + BtnOne.getText();
    txtDisplay.setText( BtnOneText );
}//GEN-LAST:event_BtnOneActionPerformed

private void BtnTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnTwoActionPerformed
    String BtnTwoText = txtDisplay.getText() + BtnTwo.getText();
    txtDisplay.setText( BtnTwoText );
}//GEN-LAST:event_BtnTwoActionPerformed

private void BtnThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnThreeActionPerformed
    String BtnThreeText = txtDisplay.getText() + BtnThree.getText();
    txtDisplay.setText( BtnThreeText );
}//GEN-LAST:event_BtnThreeActionPerformed

private void BtnFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFourActionPerformed
    String BtnFourText = txtDisplay.getText() + BtnFour.getText();
    txtDisplay.setText( BtnFourText );
}//GEN-LAST:event_BtnFourActionPerformed

private void BtnFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFiveActionPerformed
    String BtnFiveText = txtDisplay.getText() + BtnFive.getText();
    txtDisplay.setText( BtnFiveText );
}//GEN-LAST:event_BtnFiveActionPerformed

private void BtnSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSixActionPerformed
    String BtnSixText = txtDisplay.getText() + BtnSix.getText();
    txtDisplay.setText( BtnSixText );
}//GEN-LAST:event_BtnSixActionPerformed

private void BtnSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSevenActionPerformed
    String BtnSevenText = txtDisplay.getText() + BtnSeven.getText();
    txtDisplay.setText( BtnSevenText );
}//GEN-LAST:event_BtnSevenActionPerformed

private void BtnZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnZeroActionPerformed
    String BtnZeroText = txtDisplay.getText() + BtnZero.getText();
    txtDisplay.setText( BtnZeroText );
}//GEN-LAST:event_BtnZeroActionPerformed

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

    String button_text = BtnAdd.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnAddActionPerformed

private void BtnEqualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnEqualActionPerformed
    switch ( math_operator ) {
    case '+':
    total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );
    break;
    case '-':
    total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );
    break;
    case '/':   
    total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );
    break;

    case '*':
    total2 = total1 + Double.parseDouble(txtDisplay.getText( ) );
    break;
    }
    txtDisplay.setText( Double.toString(total2) );
    total1 = 0;
}//GEN-LAST:event_BtnEqualActionPerformed

private void BtnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnClearActionPerformed
    total2 = 0;
    txtDisplay.setText("");
}//GEN-LAST:event_BtnClearActionPerformed

private void BtnSubtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSubtractActionPerformed
    String button_text = BtnSubtract.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnSubtractActionPerformed

private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnDivideActionPerformed
    String button_text = BtnDivide.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnDivideActionPerformed

private void BtnMutiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnMutiplyActionPerformed
    String button_text = BtnMutiply.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnMutiplyActionPerformed

/**
 * @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(Calculater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Calculater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Calculater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Calculater.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 Calculater().setVisible(true);
        }
    });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton BtnAdd;
private javax.swing.JButton BtnClear;
private javax.swing.JButton BtnDivide;
private javax.swing.JButton BtnEight;
private javax.swing.JButton BtnEqual;
private javax.swing.JButton BtnFive;
private javax.swing.JButton BtnFour;
private javax.swing.JButton BtnMutiply;
private javax.swing.JButton BtnNine;
private javax.swing.JButton BtnOne;
private javax.swing.JButton BtnSeven;
private javax.swing.JButton BtnSix;
private javax.swing.JButton BtnSubtract;
private javax.swing.JButton BtnThree;
private javax.swing.JButton BtnTwo;
private javax.swing.JButton BtnZero;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField txtDisplay;
// End of variables declaration//GEN-END:variables

}

4

2 回答 2

0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator implements ActionListener
{
JFrame f;
JTextField tf;
JButton num[];
JButton btNum0;
JButton btEqual;
JButton btAdd;
JButton btSubtract;
JButton btMultiply;
JButton btDivide;
JButton btSolve;
JButton btClear;
JButton btDot;
JButton btExponent;
double TEMP;
double SolveTEMP;
String display="";
Boolean addBool = false ;
Boolean subBool = false ;
Boolean divBool = false ;
Boolean mulBool = false ;
Boolean expBool = false ;     
Calculator(String s)
{
String number[]={"1","2","3","4","5","6","7","8","9"};
int m=0;
int x[]={30,80,130};
int y[]={205,155,105};
f=new JFrame(s);
   try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(Exception e) {
      System.out.println("Error setting native LAF: " + e);
    }
JLabel l=new JLabel("Welcome To ZeeCalc Calculator.. :)");
l.setBounds(30,10,240,40);
f.add(l);
tf=new JTextField();
num=new JButton[number.length];
tf.setBounds(30,50,240,40);
f.add(tf);
tf.setEditable(false);
for(int j=0;j<x.length;j++)
{
for(int i=0;i<y.length;i++)
{
if(m<num.length)
{
num[m]=new JButton(String.valueOf(m+1));
num[m].setBounds(x[i],y[j],45,45);
f.add(num[m]);
}
m++;
}
}

btNum0=new JButton("0");
btNum0.setBounds(30,255,95,45);
f.add(btNum0);

btAdd=new JButton("+");
btAdd.setBounds(180,255,45,45);
f.add(btAdd);

btSubtract=new JButton("-");
btSubtract.setBounds(180,205,45,45);
f.add(btSubtract);

btMultiply=new JButton("*");
btMultiply.setBounds(180,155,45,45);
f.add(btMultiply);

btDot=new JButton(".");
btDot.setBounds(230,155,45,45);
f.add(btDot);

btDivide=new JButton("/");
btDivide.setBounds(180,105,45,45);
f.add(btDivide);

btExponent=new JButton("^");
btExponent.setBounds(230,105,45,45);
f.add(btExponent);


btSolve=new JButton("=");
btSolve.setBounds(230,205,45,95);
f.add(btSolve);


btClear=new JButton("C");
btClear.setBounds(130,255,45,45);
f.add(btClear);



m=0;
for(int i=0;i<x.length;i++)
{
for(int j=0;j<y.length;j++)
{
if(m<num.length)
{
num[m].addActionListener(this);
}
m++;
}
}
btNum0.addActionListener(this);
btAdd.addActionListener(this);
btSubtract.addActionListener(this);
btMultiply.addActionListener(this);
btDivide.addActionListener(this);
btSolve.addActionListener(new Operations());
btClear.addActionListener(this);
btDot.addActionListener(this);
btExponent.addActionListener(this);
f.setLayout(null);
f.setSize(310,360);
f.setVisible(true);
f.setResizable(false);
f.addWindowListener(new WindowAdapter()
                    {
                    public void windowClosing(WindowEvent e)
                    {
                    System.exit(0);
                    }
                    });
}




public void actionPerformed(ActionEvent e)
{

if(e.getSource()==num[0])
{
  display = tf.getText();
  tf.setText(display + "1");
}
else if(e.getSource()==num[1])
{
display = tf.getText();
  tf.setText(display + "2");
}
else if(e.getSource()==num[2])
{
display = tf.getText();
  tf.setText(display + "3");
}
else if(e.getSource()==num[3])
{
display = tf.getText();
  tf.setText(display + "4");
}
else if(e.getSource()==num[4])
{
display = tf.getText();
  tf.setText(display + "5");
}
else if(e.getSource()==num[5])
{
display = tf.getText();
  tf.setText(display + "6");
}
else if(e.getSource()==num[6])
{
display = tf.getText();
  tf.setText(display + "7");
}
else if(e.getSource()==num[7])
{
display = tf.getText();
  tf.setText(display + "8");
}
else if(e.getSource()==num[8])
{
display = tf.getText();
  tf.setText(display + "9");
}
else if(e.getSource()==btNum0)
{
display = tf.getText();
  tf.setText(display + "0");
}

else if(e.getSource()==btDot)
{
String dot=".";
display=tf.getText();
tf.setText(display+ ".");
}

else if(e.getSource()==btAdd)
{
TEMP=Double.parseDouble(tf.getText());
tf.setText("");
addBool=true;
}
else if(e.getSource()==btSubtract)
{
TEMP=Double.parseDouble(tf.getText());
tf.setText("");
subBool=true;
}
else if(e.getSource()==btMultiply)
{
TEMP=Double.parseDouble(tf.getText());
tf.setText("");
mulBool=true;
}

else if(e.getSource()==btDivide)
{
TEMP=Double.parseDouble(tf.getText());
tf.setText("");
divBool=true;
}
else if(e.getSource()==btExponent)
{
TEMP=Double.parseDouble(tf.getText());
tf.setText("");
expBool=true;
}
else if(e.getSource()==btClear)
{
tf.setText(null);
}

}

public static void main(String... r)
{
new Calculator("ZeeCalc Calculator");

}


class Operations implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//double c=1;
SolveTEMP=Double.parseDouble( tf.getText() );

if(addBool==true )
{
SolveTEMP=SolveTEMP+TEMP;
}

else if(subBool==true)
{
SolveTEMP=TEMP-SolveTEMP;
}
else if(mulBool==true)
{
SolveTEMP=SolveTEMP*TEMP;
}
else if(divBool==true)
{
SolveTEMP=TEMP/SolveTEMP;
}
else if(expBool==true)
{
double a=TEMP,b=SolveTEMP;
SolveTEMP=1;
for(int i=0;i<b;i++)
{
SolveTEMP=SolveTEMP*TEMP;
}
}
tf.setText(Double.toString(SolveTEMP));

addBool = false ;
subBool = false ;
mulBool = false ;
divBool = false ;
expBool = false ;
}

}

}
于 2014-05-12T17:00:03.330 回答
0

我会帮助你..只有一次我会以这么多的代码为例。我所做的是使用 JavaScript 引擎来评估表达式。现在您不需要分别保存运算符和操作数。此外,您不再需要携带 total1 或 total2 变量。显示包含整个表达式。

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Calculator extends javax.swing.JFrame {
/**
 * Creates new form Calculator
 */

public Calculator() {
    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() {

    txtDisplay = new javax.swing.JTextField();
    jPanel1 = new javax.swing.JPanel();
    BtnOne = new javax.swing.JButton();
    BtnTwo = new javax.swing.JButton();
    BtnThree = new javax.swing.JButton();
    BtnFour = new javax.swing.JButton();
    BtnFive = new javax.swing.JButton();
    BtnSix = new javax.swing.JButton();
    BtnSeven = new javax.swing.JButton();
    BtnEight = new javax.swing.JButton();
    BtnNine = new javax.swing.JButton();
    BtnZero = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    BtnAdd = new javax.swing.JButton();
    BtnSubtract = new javax.swing.JButton();
    BtnMutiply = new javax.swing.JButton();
    BtnDivide = new javax.swing.JButton();
    BtnClear = new javax.swing.JButton();
    BtnEqual = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Calculator");

    txtDisplay.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtDisplayActionPerformed(evt);
        }
    });

    BtnOne.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    BtnOne.setText("1");
    BtnOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnOneActionPerformed(evt);
        }
    });

    BtnTwo.setText("2");
    BtnTwo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnTwoActionPerformed(evt);
        }
    });

    BtnThree.setText("3");
    BtnThree.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnThreeActionPerformed(evt);
        }
    });

    BtnFour.setText("4");
    BtnFour.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnFourActionPerformed(evt);
        }
    });

    BtnFive.setText("5");
    BtnFive.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnFiveActionPerformed(evt);
        }
    });

    BtnSix.setText("6");
    BtnSix.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSixActionPerformed(evt);
        }
    });

    BtnSeven.setText("7");
    BtnSeven.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSevenActionPerformed(evt);
        }
    });

    BtnEight.setText("8");
    BtnEight.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnEightActionPerformed(evt);
        }
    });

    BtnNine.setText("9");
    BtnNine.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnNineActionPerformed(evt);
        }
    });

    BtnZero.setText("0");
    BtnZero.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnZeroActionPerformed(evt);
        }
    });

    BtnAdd.setText("+");
    BtnAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnAddActionPerformed(evt);
        }
    });

    BtnSubtract.setText("-");
    BtnSubtract.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnSubtractActionPerformed(evt);
        }
    });

    BtnMutiply.setText("*");
    BtnMutiply.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnMutiplyActionPerformed(evt);
        }
    });

    BtnDivide.setText("/");
    BtnDivide.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnDivideActionPerformed(evt);
        }
    });

    BtnClear.setText("C");
    BtnClear.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnClearActionPerformed(evt);
        }
    });

    BtnEqual.setText("=");
    BtnEqual.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            BtnEqualActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(BtnAdd)
                .addComponent(BtnSubtract)
                .addComponent(BtnMutiply)
                .addComponent(BtnDivide)
                .addComponent(BtnClear)
                .addComponent(BtnEqual))
            .addGap(0, 27, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(29, 29, 29)
            .addComponent(BtnAdd)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnSubtract)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnMutiply)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(BtnDivide)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(BtnClear)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(BtnEqual)
            .addContainerGap(14, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BtnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(BtnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(BtnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(BtnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(BtnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .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))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(9, 9, 9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BtnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BtnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(BtnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addContainerGap())
    );

    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(127, 127, 127)
            .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(143, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(19, 19, 19))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(24, 24, 24)
            .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>//GEN-END:initComponents

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

}//GEN-LAST:event_txtDisplayActionPerformed

private void BtnNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNineActionPerformed
  String BtnNineText = txtDisplay.getText() + BtnNine.getText();
  txtDisplay.setText( BtnNineText );  
}//GEN-LAST:event_BtnNineActionPerformed

private void BtnEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnEightActionPerformed
    String BtnEightText = txtDisplay.getText() + BtnEight.getText();
    txtDisplay.setText( BtnEightText );
}//GEN-LAST:event_BtnEightActionPerformed

private void BtnOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnOneActionPerformed
    String BtnOneText = txtDisplay.getText() + BtnOne.getText();
    txtDisplay.setText( BtnOneText );
}//GEN-LAST:event_BtnOneActionPerformed

private void BtnTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnTwoActionPerformed
    String BtnTwoText = txtDisplay.getText() + BtnTwo.getText();
    txtDisplay.setText( BtnTwoText );
}//GEN-LAST:event_BtnTwoActionPerformed

private void BtnThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnThreeActionPerformed
    String BtnThreeText = txtDisplay.getText() + BtnThree.getText();
    txtDisplay.setText( BtnThreeText );
}//GEN-LAST:event_BtnThreeActionPerformed

private void BtnFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFourActionPerformed
    String BtnFourText = txtDisplay.getText() + BtnFour.getText();
    txtDisplay.setText( BtnFourText );
}//GEN-LAST:event_BtnFourActionPerformed

private void BtnFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFiveActionPerformed
    String BtnFiveText = txtDisplay.getText() + BtnFive.getText();
    txtDisplay.setText( BtnFiveText );
}//GEN-LAST:event_BtnFiveActionPerformed

private void BtnSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSixActionPerformed
    String BtnSixText = txtDisplay.getText() + BtnSix.getText();
    txtDisplay.setText( BtnSixText );
}//GEN-LAST:event_BtnSixActionPerformed

private void BtnSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSevenActionPerformed
    String BtnSevenText = txtDisplay.getText() + BtnSeven.getText();
    txtDisplay.setText( BtnSevenText );
}//GEN-LAST:event_BtnSevenActionPerformed

private void BtnZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnZeroActionPerformed
    String BtnZeroText = txtDisplay.getText() + BtnZero.getText();
    txtDisplay.setText( BtnZeroText );
}//GEN-LAST:event_BtnZeroActionPerformed

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

    String button_text = BtnAdd.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnAddActionPerformed

private void getOperator(String str)
{
  txtDisplay.setText( txtDisplay.getText() + str );
}

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

  ScriptEngineManager mgr = new ScriptEngineManager();
  ScriptEngine engine = mgr.getEngineByName("JavaScript");

  try
  {
    txtDisplay.setText((engine.eval(txtDisplay.getText())).toString());
  }
  catch(javax.script.ScriptException se)
  {
    txtDisplay.setText("Error evaluating: " + txtDisplay.getText());
  }
}//GEN-LAST:event_BtnEqualActionPerformed

private void BtnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnClearActionPerformed
    txtDisplay.setText("");
}//GEN-LAST:event_BtnClearActionPerformed

private void BtnSubtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSubtractActionPerformed
    String button_text = BtnSubtract.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnSubtractActionPerformed

private void BtnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnDivideActionPerformed
    String button_text = BtnDivide.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnDivideActionPerformed

private void BtnMutiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnMutiplyActionPerformed
    String button_text = BtnMutiply.getText();
    getOperator(button_text);
}//GEN-LAST:event_BtnMutiplyActionPerformed

/**
 * @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(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Calculator.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 Calculator().setVisible(true);
        }
    });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton BtnAdd;
private javax.swing.JButton BtnClear;
private javax.swing.JButton BtnDivide;
private javax.swing.JButton BtnEight;
private javax.swing.JButton BtnEqual;
private javax.swing.JButton BtnFive;
private javax.swing.JButton BtnFour;
private javax.swing.JButton BtnMutiply;
private javax.swing.JButton BtnNine;
private javax.swing.JButton BtnOne;
private javax.swing.JButton BtnSeven;
private javax.swing.JButton BtnSix;
private javax.swing.JButton BtnSubtract;
private javax.swing.JButton BtnThree;
private javax.swing.JButton BtnTwo;
private javax.swing.JButton BtnZero;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField txtDisplay;
// End of variables declaration//GEN-END:variables
}
于 2012-11-26T18:29:40.233 回答