我正在制作一个计算器,但我不知道该放什么 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
}