Como hacer una calculadora en NetBeans.
Se llevara en tres pasos, diseño, codificación, uso.
Diseño:
Hacer un Jframe y arrastras los botones, diseñar a nuestro gusto en mi caso me quedo asi.
utilice 11 button para los numeros, un button para el borrar C, y cinco button para dividir, sumar, restar, multiplicar, y el igual.
Tambien una label para poner el autor By Sarmiento, y un text field para que aparezca el resultado.
Codificacion
este es el código de toda mi calculadora.
En mi caso a mi paquete le llame jcalculator
Como buen programador sabrás que descartar, y que copiar y pegar en tu programa.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jcalculator;
/**
*
* @author sarmiento
*/
public class JavaCalculator extends javax.swing.JFrame {
private double total1 = 0.0;
private double total2 = 0.0;
private char math_operator;
/**
* Creates new form JavaCalculator
*/
public JavaCalculator() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollBar1 = new javax.swing.JScrollBar();
jColorChooser1 = new javax.swing.JColorChooser();
jColorChooser2 = new javax.swing.JColorChooser();
jColorChooser3 = new javax.swing.JColorChooser();
jColorChooser4 = new javax.swing.JColorChooser();
jColorChooser5 = new javax.swing.JColorChooser();
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();
bntPoint = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
btnEquals = new javax.swing.JButton();
btnSubtract = new javax.swing.JButton();
btnPlus = new javax.swing.JButton();
btnDivide = new javax.swing.JButton();
btnMultiply = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculator");
txtDisplay.setForeground(new java.awt.Color(255, 0, 0));
txtDisplay.setCaretColor(new java.awt.Color(255, 255, 51));
txtDisplay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtDisplayActionPerformed(evt);
}
});
btnOne.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnOne.setForeground(new java.awt.Color(102, 0, 0));
btnOne.setText("1");
btnOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOneActionPerformed(evt);
}
});
btnTwo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnTwo.setForeground(new java.awt.Color(102, 0, 102));
btnTwo.setText("2");
btnTwo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTwoActionPerformed(evt);
}
});
btnThree.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnThree.setForeground(new java.awt.Color(0, 204, 0));
btnThree.setText("3");
btnThree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnThreeActionPerformed(evt);
}
});
btnFour.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnFour.setForeground(new java.awt.Color(0, 153, 153));
btnFour.setText("4");
btnFour.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFourActionPerformed(evt);
}
});
btnFive.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnFive.setForeground(new java.awt.Color(255, 102, 153));
btnFive.setText("5");
btnFive.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFiveActionPerformed(evt);
}
});
btnSix.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnSix.setForeground(new java.awt.Color(0, 204, 204));
btnSix.setText("6");
btnSix.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSixActionPerformed(evt);
}
});
btnSeven.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnSeven.setForeground(new java.awt.Color(255, 102, 0));
btnSeven.setText("7");
btnSeven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSevenActionPerformed(evt);
}
});
btnEight.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnEight.setForeground(new java.awt.Color(255, 0, 51));
btnEight.setText("8");
btnEight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEightActionPerformed(evt);
}
});
btnNine.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnNine.setText("9");
btnNine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNineActionPerformed(evt);
}
});
btnZero.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnZero.setText("0");
btnZero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnZeroActionPerformed(evt);
}
});
bntPoint.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bntPoint.setText(".");
bntPoint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bntPointActionPerformed(evt);
}
});
jPanel2.setForeground(new java.awt.Color(153, 255, 153));
btnEquals.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
btnEquals.setForeground(new java.awt.Color(0, 51, 51));
btnEquals.setText("=");
btnEquals.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEqualsActionPerformed(evt);
}
});
btnSubtract.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnSubtract.setText("-");
btnSubtract.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSubtractActionPerformed(evt);
}
});
btnPlus.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnPlus.setText("+");
btnPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPlusActionPerformed(evt);
}
});
btnDivide.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnDivide.setText("/");
btnDivide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDivideActionPerformed(evt);
}
});
btnMultiply.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnMultiply.setText("*");
btnMultiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnMultiplyActionPerformed(evt);
}
});
btnDelete.setFont(new java.awt.Font("Narkisim", 1, 14)); // NOI18N
btnDelete.setForeground(new java.awt.Color(255, 0, 204));
btnDelete.setText("C");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(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()
.addContainerGap()
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnEquals, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(btnSubtract, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDivide, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(19, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(11, 11, 11)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnDivide, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSubtract, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(btnEquals, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
jLabel1.setText("By Sarmiento");
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(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bntPoint, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
.addGap(39, 39, 39))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(btnSeven, 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))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(btnFour, 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))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(btnNine, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, Short.MAX_VALUE)))
.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()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnTwo, 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)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnSix, 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(btnSeven, 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(btnNine, 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(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bntPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
btnOne.getAccessibleContext().setAccessibleDescription("");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnFourText = txtDisplay.getText () + btnFour.getText ();
txtDisplay.setText (btnFourText);
}
private void txtDisplayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnOneText = txtDisplay.getText () + btnOne.getText ();
txtDisplay.setText (btnOneText);
}
private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnTwoText = txtDisplay.getText () + btnTwo.getText ();
txtDisplay.setText (btnTwoText);
}
private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnThreeText = txtDisplay.getText () + btnThree.getText ();
txtDisplay.setText (btnThreeText);
}
private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnFiveText = txtDisplay.getText () + btnFive.getText ();
txtDisplay.setText (btnFiveText);
}
private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnSixText = txtDisplay.getText () + btnSix.getText ();
txtDisplay.setText (btnSixText);
}
private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnSevenText = txtDisplay.getText () + btnSeven.getText ();
txtDisplay.setText (btnSevenText);
}
private void btnEightActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnEightText = txtDisplay.getText () + btnEight.getText ();
txtDisplay.setText (btnEightText);
}
private void btnNineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnNineText = txtDisplay.getText () + btnNine.getText ();
txtDisplay.setText (btnNineText);
}
private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnZeroText = txtDisplay.getText () + btnZero.getText ();
txtDisplay.setText (btnZeroText);
}
private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String button_text = btnPlus.getText ();
getOperator (button_text);
}
private void getOperator(String btnText) {
math_operator = btnText.charAt (0);
total1 = total1 + Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
}
private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
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;
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
//
total2 = 0;
txtDisplay.setText ("");
}
private void btnSubtractActionPerformed(java.awt.event.ActionEvent evt) {
//TODO add your handling code here:
String button_text = btnSubtract.getText ();
getOperator (button_text);
}
private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String button_text = btnDivide.getText ();
getOperator (button_text);
}
private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String button_text = btnMultiply.getText ();
getOperator (button_text);
}
private void bntPointActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String btnPointText = txtDisplay.getText () + bntPoint.getText ();
txtDisplay.setText (btnPointText);
}
/**
* @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(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JavaCalculator.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 JavaCalculator().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bntPoint;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnDivide;
private javax.swing.JButton btnEight;
private javax.swing.JButton btnEquals;
private javax.swing.JButton btnFive;
private javax.swing.JButton btnFour;
private javax.swing.JButton btnMultiply;
private javax.swing.JButton btnNine;
private javax.swing.JButton btnOne;
private javax.swing.JButton btnPlus;
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.JColorChooser jColorChooser1;
private javax.swing.JColorChooser jColorChooser2;
private javax.swing.JColorChooser jColorChooser3;
private javax.swing.JColorChooser jColorChooser4;
private javax.swing.JColorChooser jColorChooser5;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollBar jScrollBar1;
private javax.swing.JTextField txtDisplay;
// End of variables declaration
}
APLICACIÓN:
Y YA ESTA LISTA NUSTRA APLICACIÓN CALCULADORA.