import javax.swing.*;
import java.awt.event.*;
import java.util.HashSet;
/*
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author JOSUE
*/
public class Calculadora extends javax.swing.JFrame {
private Entrada ultimaEntrada;
private boolean puntoDecimal;
private char operador;
private byte numOperandos;
private double operando1,operando2;
private enum Entrada{NINGUNA,DIGITO,OPERADOR,CE};
/**
* Creates new form Calculadora
*/
public Calculadora()
{
initComponents();
ultimaEntrada= Entrada.NINGUNA;
puntoDecimal=false;
numOperandos=0;
operando1=0;
operando2=0;
}
/**
* 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() {
jbtDigito18 = new javax.swing.JButton();
jtfPantalla = new javax.swing.JTextField();
jbtDigito8 = new javax.swing.JButton();
jbtDigito7 = new javax.swing.JButton();
jbtDigito9 = new javax.swing.JButton();
jbtDigito4 = new javax.swing.JButton();
jbtDigito5 = new javax.swing.JButton();
jbtDigito6 = new javax.swing.JButton();
jbtDigito2 = new javax.swing.JButton();
jbtDigito3 = new javax.swing.JButton();
jbtDigito1 = new javax.swing.JButton();
jbtDigito0 = new javax.swing.JButton();
jbtDigitoPunto = new javax.swing.JButton();
jbtDividir = new javax.swing.JButton();
jbtInicial = new javax.swing.JButton();
jbtMenos = new javax.swing.JButton();
jbtMas = new javax.swing.JButton();
jbtBorrarEntrada = new javax.swing.JButton();
jbtIgual = new javax.swing.JButton();
jbtPor = new javax.swing.JButton();
jbtTantoPorCiento = new javax.swing.JButton();
jbtDigito18.setText("9");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculadora");
setBackground(new java.awt.Color(204, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
jtfPantalla.setEditable(false);
jtfPantalla.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
jtfPantalla.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jtfPantalla.setText("0.");
jbtDigito8.setText("8");
jbtDigito8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito7.setText("7");
jbtDigito7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito9.setText("9");
jbtDigito9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito4.setText("4");
jbtDigito4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito5.setText("5");
jbtDigito5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito6.setText("6");
jbtDigito6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito2.setText("2");
jbtDigito2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito3.setText("3");
jbtDigito3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito1.setText("1");
jbtDigito1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigito0.setText("0");
jbtDigito0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoActionPerformed(evt);
}
});
jbtDigitoPunto.setText(".");
jbtDigitoPunto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtDigitoPuntoActionPerformed(evt);
}
});
jbtDividir.setText("/");
jbtDividir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtOperacionActionPerformed(evt);
}
});
jbtInicial.setText("C");
jbtInicial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtInicialActionPerformed(evt);
}
});
jbtMenos.setText("-");
jbtMenos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtOperacionActionPerformed(evt);
}
});
jbtMas.setText("+");
jbtMas.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtOperacionActionPerformed(evt);
}
});
jbtBorrarEntrada.setText("CE");
jbtBorrarEntrada.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtBorrarEntrada(evt);
}
});
jbtIgual.setText("=");
jbtIgual.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtOperacionActionPerformed(evt);
}
});
jbtPor.setText("X");
jbtPor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtOperacionActionPerformed(evt);
}
});
jbtTantoPorCiento.setText("%");
jbtTantoPorCiento.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtTantoPorCientoActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jbtDigito0, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtDigitoPunto, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jbtDigito7, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtDigito8, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jbtDigito4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtDigito5, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jbtDigito1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jbtDigito2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jbtDigito6, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito9, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jbtDividir, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtMenos, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addGroup(layout.createSequentialGroup()
.addComponent(jbtPor, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jbtMas, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(jbtTantoPorCiento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jbtInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbtBorrarEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addComponent(jbtIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jtfPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jtfPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtDigito7, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtBorrarEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtDigito6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtMenos, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDividir, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtDigito3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigito1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jbtDigito0, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtDigitoPunto, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtMas, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtPor, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbtTantoPorCiento, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbtIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jbtDigitoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDigitoActionPerformed
// TODO add your handling code here:
JButton objJBT=(JButton)evt.getSource();
String textoBoton=objJBT.getText();
if(ultimaEntrada !=Entrada.DIGITO)
{
if(textoBoton.compareTo("0")==0)return;
jtfPantalla.setText("");
ultimaEntrada=Entrada.DIGITO;
}
jtfPantalla.setText(jtfPantalla.getText() + textoBoton);
}//GEN-LAST:event_jbtDigitoActionPerformed
private void jbtDigitoPuntoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDigitoPuntoActionPerformed
// TODO add your handling code here:
if(ultimaEntrada !=Entrada.DIGITO)
{
jtfPantalla.setText("0.");
ultimaEntrada=Entrada.DIGITO;
}
else if(puntoDecimal==false)
jtfPantalla.setText(jtfPantalla.getText()+ ".");
puntoDecimal=true;
}//GEN-LAST:event_jbtDigitoPuntoActionPerformed
private void jbtOperacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtOperacionActionPerformed
// TODO add your handling code here:
JButton objJBT=(JButton)evt.getSource();
String textoBoton=objJBT.getText();
if((numOperandos == 0) && (textoBoton.compareTo("-")==0))
ultimaEntrada = Entrada.DIGITO;
if (ultimaEntrada == Entrada.DIGITO)
numOperandos++;
if(numOperandos==1)
operando1=Double.parseDouble(jtfPantalla.getText());
else if(numOperandos==2)
{
operando2=Double.parseDouble(jtfPantalla.getText());
switch (operador)
{
case '+':
operando1 +=operando2;
break;
case '-':
operando1 -=operando2;
break;
case 'X':
operando1 *= operando2;
break;
case '/':
operando1/=operando2;
break;
case '=':
operando1=operando2;
break;
}
jtfPantalla.setText(Double.toString(operando1));
numOperandos=1;
}
operador=textoBoton.charAt(0);
ultimaEntrada=Entrada.OPERADOR;
}//GEN-LAST:event_jbtOperacionActionPerformed
private void jbtTantoPorCientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtTantoPorCientoActionPerformed
// TODO add your handling code here:
double resultado;
if(ultimaEntrada==Entrada.DIGITO)
{
resultado=operando1*Double.parseDouble(jtfPantalla.getText())/100;
jtfPantalla.setText(Double.toString(resultado));
jbtIgual.doClick();
jbtTantoPorCiento.requestFocus();
}
}//GEN-LAST:event_jbtTantoPorCientoActionPerformed
private void jbtInicialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtInicialActionPerformed
// TODO add your handling code here:
jtfPantalla.setText("0.");
ultimaEntrada=Entrada.NINGUNA;
puntoDecimal=false;
operador=0;
numOperandos=0;
operando1=0;
operando2=0;
}//GEN-LAST:event_jbtInicialActionPerformed
private void jbtBorrarEntrada(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBorrarEntrada
// TODO add your handling code here:
jtfPantalla.setText("0.");
ultimaEntrada=Entrada.CE;
puntoDecimal=false;
}//GEN-LAST:event_jbtBorrarEntrada
/**
* @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(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculadora.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 Calculadora().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jbtBorrarEntrada;
private javax.swing.JButton jbtDigito0;
private javax.swing.JButton jbtDigito1;
private javax.swing.JButton jbtDigito18;
private javax.swing.JButton jbtDigito2;
private javax.swing.JButton jbtDigito3;
private javax.swing.JButton jbtDigito4;
private javax.swing.JButton jbtDigito5;
private javax.swing.JButton jbtDigito6;
private javax.swing.JButton jbtDigito7;
private javax.swing.JButton jbtDigito8;
private javax.swing.JButton jbtDigito9;
private javax.swing.JButton jbtDigitoPunto;
private javax.swing.JButton jbtDividir;
private javax.swing.JButton jbtIgual;
private javax.swing.JButton jbtInicial;
private javax.swing.JButton jbtMas;
private javax.swing.JButton jbtMenos;
private javax.swing.JButton jbtPor;
private javax.swing.JButton jbtTantoPorCiento;
private javax.swing.JTextField jtfPantalla;
// End of variables declaration//GEN-END:variables
}