/*
* calculadoraWin.java
*
* Created on 13 de marzo de 2003, 21:49
*/

/**
*
* @author Fernando Olvera Carrillo
*/
public class calculadoraWin extends javax.swing.JFrame {

/** Creates new form calculadoraWin */
public calculadoraWin() {
initComponents();
setSize(370,230);
cboOperaciones.addItem("Suma");
cboOperaciones.addItem("Resta");
cboOperaciones.addItem("Multiplicacion");
cboOperaciones.addItem("Division");
}

/** 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.
*/
private void initComponents() {//GEN-BEGIN:initComponents
txtNum1 = new javax.swing.JTextField();
txtNum2 = new javax.swing.JTextField();
lblOperador = new javax.swing.JLabel();
cboOperaciones = new javax.swing.JComboBox();
btnDoOper = new javax.swing.JButton();
txtResult = new javax.swing.JTextField();
lblIgual = new javax.swing.JLabel();
lblTipOper = new javax.swing.JLabel();
lblInstrucciones = new javax.swing.JLabel();
mnbMenu = new javax.swing.JMenuBar();
menProg = new javax.swing.JMenu();
mniSalir = new javax.swing.JMenuItem();
menAyuda = new javax.swing.JMenu();
mniAcerca = new javax.swing.JMenuItem();

getContentPane().setLayout(null);

setTitle("JCalc");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

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

getContentPane().add(txtNum1);
txtNum1.setBounds(20, 90, 70, 20);

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

getContentPane().add(txtNum2);
txtNum2.setBounds(130, 90, 70, 20);

lblOperador.setText("Tipo de operaci\u00f3n a realizar");
getContentPane().add(lblOperador);
lblOperador.setBounds(20, 25, 160, 16);

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

getContentPane().add(cboOperaciones);
cboOperaciones.setBounds(180, 20, 120, 25);

btnDoOper.setText("Realizar operaci\u00f3n");
btnDoOper.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDoOperActionPerformed(evt);
}
});

getContentPane().add(btnDoOper);
btnDoOper.setBounds(120, 130, 140, 26);

getContentPane().add(txtResult);
txtResult.setBounds(240, 90, 100, 20);

lblIgual.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblIgual.setText("=");
lblIgual.setBorder(new javax.swing.border.EtchedBorder());
getContentPane().add(lblIgual);
lblIgual.setBounds(210, 90, 20, 20);

lblTipOper.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTipOper.setText("+");
lblTipOper.setBorder(new javax.swing.border.EtchedBorder());
getContentPane().add(lblTipOper);
lblTipOper.setBounds(100, 90, 20, 20);

lblInstrucciones.setFont(new java.awt.Font("Dialog", 0, 12));
lblInstrucciones.setText("Escriba el operador y el operando para realizar la operaci\u00f3n");
getContentPane().add(lblInstrucciones);
lblInstrucciones.setBounds(20, 60, 330, 16);

menProg.setText("Programa");
mniSalir.setText("Salir");
mniSalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mniSalirActionPerformed(evt);
}
});

menProg.add(mniSalir);
mnbMenu.add(menProg);
menAyuda.setText("Ayuda");
mniAcerca.setText("Acerca de...");
mniAcerca.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mniAcercaActionPerformed(evt);
}
});

menAyuda.add(mniAcerca);
mnbMenu.add(menAyuda);
setJMenuBar(mnbMenu);

pack();
}//GEN-END:initComponents

private void btnDoOperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoOperActionPerformed
// javax.swing.JOptionPane.showMessageDialog(this, cboOperaciones.getSelectedItem()+"\nNum1 = "+txtNum1.getText()+"\nNum2 = "+txtNum2.getText(), "Debug",javax.swing.JOptionPane.INFORMATION_MESSAGE);
//String cadena;
//cadena = txtNum1.getText();
try{
Float fNum1, fNum2;
fNum1 = new Float( txtNum1.getText() );
fNum2 = new Float( txtNum2.getText() );
Calculadora miCalc = new Calculadora( fNum1.floatValue(), fNum2.floatValue() );
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Suma") )
txtResult.setText( miCalc.suma() );
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Resta") )
txtResult.setText( miCalc.resta() );
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Multiplicacion") )
txtResult.setText( miCalc.multiplica() );
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Division") )
txtResult.setText( miCalc.divide() );
} // fin del try
catch( NumberFormatException NFEx ){
javax.swing.JOptionPane.showMessageDialog(this,
"Introduce solo numeros","Debug", javax.swing.JOptionPane.ERROR_MESSAGE );
if ( NFEx.toString().endsWith("\""+txtNum1.getText()+"\"") ){
txtNum1.requestFocus();
txtNum1.selectAll();
} // fin de if ( NFEx.toString().endsWith("\""+txtNum1.getText()+"\"") )
else{
txtNum2.requestFocus();
txtNum2.selectAll();
} // fin del else
} // fin del catch
}//GEN-LAST:event_btnDoOperActionPerformed

private void cboOperacionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboOperacionesActionPerformed
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Suma") )
lblTipOper.setText("+");
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Resta") )
lblTipOper.setText("-");
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Multiplicacion") )
lblTipOper.setText("*");
if ( cboOperaciones.getSelectedItem().toString().equalsIgnoreCase("Division") )
lblTipOper.setText("/");
}//GEN-LAST:event_cboOperacionesActionPerformed

private void txtNum2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNum2ActionPerformed
// Add your handling code here:
}//GEN-LAST:event_txtNum2ActionPerformed

private void txtNum1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNum1ActionPerformed
// Add your handling code here:
}//GEN-LAST:event_txtNum1ActionPerformed

private void mniSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniSalirActionPerformed
System.exit(0);
}//GEN-LAST:event_mniSalirActionPerformed

private void mniAcercaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniAcercaActionPerformed
javax.swing.JOptionPane.showMessageDialog(this,
"Fernando Olvera Carrillo - 0120120648\nCalculadora v0.1a Build 70\nLPJ 13 mar-2003",
"Acerca de...",javax.swing.JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_mniAcercaActionPerformed

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new calculadoraWin().show();
}


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem mniSalir;
private javax.swing.JTextField txtNum2;
private javax.swing.JTextField txtNum1;
private javax.swing.JComboBox cboOperaciones;
private javax.swing.JMenuItem mniAcerca;
private javax.swing.JButton btnDoOper;
private javax.swing.JLabel lblTipOper;
private javax.swing.JTextField txtResult;
private javax.swing.JLabel lblIgual;
private javax.swing.JLabel lblOperador;
private javax.swing.JMenuBar mnbMenu;
private javax.swing.JLabel lblInstrucciones;
private javax.swing.JMenu menProg;
private javax.swing.JMenu menAyuda;
// End of variables declaration//GEN-END:variables

}

class Calculadora{
private double Num1, Num2;

public Calculadora( double num1, double num2 ){
this.Num1 = num1;
this.Num2 = num2;
} // fin de Operaciones

public String suma(){
return java.lang.String.valueOf( this.Num1 + this.Num2 );
}

public String resta(){
return java.lang.String.valueOf( this.Num1 - this.Num2 );
}
public String multiplica(){
return java.lang.String.valueOf( this.Num1 * this.Num2 );
} // fin de multiplica

public String divide(){
return java.lang.String.valueOf( this.Num1 / this.Num2 );
} // fin de divide

} // fin de la clase Calculadora