jueves, 13 de octubre de 2011

Demo 1

Este es el primer demo que presente en la clase. Como habia dicho anteriormente el principal objetivo que yo tenía para medio curso, era realizar una calculadora con las cuatro operaciones básicas. El código es el siguiente:
public class interfaz extends javax.swing.JFrame {

    float var1,var2;
    boolean suma,resta,mult,div;
    public interfaz() {
        initComponents();
 //se declara como falso porque aun no se hace ninguna operacion
        suma=false;
        resta=false;
        mult=false;
        div=false;
    }
 
    private void initComponents() {

        textoRESULTADO = new javax.swing.JTextField();
        numero1 = new javax.swing.JButton();
        numero2 = new javax.swing.JButton();
        numero3 = new javax.swing.JButton();
        numero6 = new javax.swing.JButton();
        numero5 = new javax.swing.JButton();
        numero4 = new javax.swing.JButton();
        numero9 = new javax.swing.JButton();
        numero8 = new javax.swing.JButton();
        numero7 = new javax.swing.JButton();
        numero0 = new javax.swing.JButton();
        botonIGUAL = new javax.swing.JButton();
        botonSUMAR = new javax.swing.JButton();
        botonRESTAR = new javax.swing.JButton();
        botonDIV = new javax.swing.JButton();
        botonMULT = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        botonLIMPIAR = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
  setTitle("Calculadora de Eduardo");
  
        numero1.setText("1");
        numero1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numero1ActionPerformed(evt);
            }
        });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        

        botonLIMPIAR.setText("clear");
        botonLIMPIAR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLIMPIARActionPerformed(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()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(botonLIMPIAR))
                    .addComponent(textoRESULTADO, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(numero4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numero5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numero6, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(numero7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numero8, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numero9, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(numero0, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(numero1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(numero2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(numero3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(botonIGUAL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(botonRESTAR, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(botonSUMAR, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                            .addComponent(botonDIV, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(botonMULT, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(botonLIMPIAR))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textoRESULTADO, 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)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numero7)
                    .addComponent(numero8)
                    .addComponent(numero9)
                    .addComponent(botonSUMAR))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numero4)
                    .addComponent(numero5)
                    .addComponent(numero6)
                    .addComponent(botonRESTAR))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numero1)
                    .addComponent(numero2)
                    .addComponent(numero3)
                    .addComponent(botonDIV))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numero0)
                    .addComponent(botonIGUAL)
                    .addComponent(botonMULT))
                .addContainerGap())
        );

        pack();
    }

    private void numero0ActionPerformed(java.awt.event.ActionEvent evt) {

        if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("0");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("0"));
        var1=Float.parseFloat(textoRESULTADO.getText());
    }//GEN-LAST:event_numero0ActionPerformed

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

          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("1");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("1"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());
    }//GEN-LAST:event_numero1ActionPerformed

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

        if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("2");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("2"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());

    }//GEN-LAST:event_numero2ActionPerformed

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

          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("3");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("3"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());

    }//GEN-LAST:event_numero3ActionPerformed

    private void botonSUMARActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSUMARActionPerformed
    //Este es el boton de SUMA por lo tanto la operacion suma es verdadera
        this.suma=true;
        this.textoRESULTADO.setText("");
        var2=var1;
        // TODO add your handling code here:
    }//GEN-LAST:event_botonSUMARActionPerformed

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

        //este es el boton IGUAL, dependiendo de la operacion verdadera pendiente, se hara la operacion
        if(suma)
        {
            //se inserta en la caja de texto el valor String para la suma de la variable 1 y 2
            this.textoRESULTADO.setText(String.valueOf(var1+var2));
            var1=var2+var1;//la variable 1 es el resultado de la operacion anterior, para poder realizar futuras operaciones
            suma=false;//se ha realizado la operacion por lo tanto este valor vuelve a ser falso para evitar conflictos

        }
        //continuamos de la misma forma ocn las demas operaciones
        if(resta)
        {
            //se inserta en la caja de texto el valor String para la suma de la variable 1 y 2
            this.textoRESULTADO.setText(String.valueOf(var2-var1));
            var1=var2-var1;//la variable 1 es el resultado de la operacion anterior, para poder realizar futuras operaciones
            resta=false;//se ha realizado la operacion por lo tanto este valor vuelve a ser falso para evitar conflictos

        }
        if(mult)
        {
            //se inserta en la caja de texto el valor String para la suma de la variable 1 y 2
            this.textoRESULTADO.setText(String.valueOf(var2*var1));
            var1=var2*var1;//la variable 1 es el resultado de la operacion anterior, para poder realizar futuras operaciones
            mult=false;//se ha realizado la operacion por lo tanto este valor vuelve a ser falso para evitar conflictos

        }
        if(div)
        {
            //se inserta en la caja de texto el valor String para la suma de la variable 1 y 2
            this.textoRESULTADO.setText(String.valueOf(var2/var1));
            var1=var2/var1;//la variable 1 es el resultado de la operacion anterior, para poder realizar futuras operaciones
            div=false;//se ha realizado la operacion por lo tanto este valor vuelve a ser falso para evitar conflictos

        }
    }//GEN-LAST:event_botonIGUALActionPerformed

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

            this.resta=true;
            this.textoRESULTADO.setText("");
            var2=var1;
// TODO add your handling code here:
    }//GEN-LAST:event_botonRESTARActionPerformed

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

        this.div=true;
        this.textoRESULTADO.setText("");
        var2=var1;// TODO add your handling code here:
    }//GEN-LAST:event_botonDIVActionPerformed

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

        this.mult=true;
        this.textoRESULTADO.setText("");
        var2=var1;// TODO add your handling code here:
    }//GEN-LAST:event_botonMULTActionPerformed

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

        //botton de CLEAR o LIMPIAR
        this.textoRESULTADO.setText("0");
        var1=0;
        var2=0;
        mult=false;
        div=false;
        suma=false;
        resta=false;
        // TODO add your handling code here:
    }//GEN-LAST:event_botonLIMPIARActionPerformed

    private void numero4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numero4ActionPerformed
  if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("4");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("4"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());        // TODO add your handling code here:
    }//GEN-LAST:event_numero4ActionPerformed

    private void numero5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numero5ActionPerformed
        // TODO add your handling code here:
          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("5");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("5"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());
    }//GEN-LAST:event_numero5ActionPerformed

    private void numero6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numero6ActionPerformed
        // TODO add your handling code here:
          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("6");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("6"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());
    }//GEN-LAST:event_numero6ActionPerformed

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

          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("7");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("7"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());
        // TODO add your handling code here:
    }//GEN-LAST:event_numero7ActionPerformed

    private void numero8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numero8ActionPerformed
        // TODO add your handling code here:
          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("8");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("8"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());
    }//GEN-LAST:event_numero8ActionPerformed

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

          if(textoRESULTADO.equals("0") || textoRESULTADO.equals(""))
            textoRESULTADO.setText("9");
        else
            textoRESULTADO.setText(textoRESULTADO.getText().concat("9"));//concatena el numero en la barra
        //asigna a la variable el valor numerico de la caja de texto, convierte de String a float
        var1=Float.parseFloat(textoRESULTADO.getText());// TODO add your handling code here:
    }//GEN-LAST:event_numero9ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new interfaz().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonDIV;
    private javax.swing.JButton botonIGUAL;
    private javax.swing.JButton botonLIMPIAR;
    private javax.swing.JButton botonMULT;
    private javax.swing.JButton botonRESTAR;
    private javax.swing.JButton botonSUMAR;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton numero0;
    private javax.swing.JButton numero1;
    private javax.swing.JButton numero2;
    private javax.swing.JButton numero3;
    private javax.swing.JButton numero4;
    private javax.swing.JButton numero5;
    private javax.swing.JButton numero6;
    private javax.swing.JButton numero7;
    private javax.swing.JButton numero8;
    private javax.swing.JButton numero9;
    private javax.swing.JTextField textoRESULTADO;
    // End of variables declaration//GEN-END:variables

}
Las imágenes de mi demo:

No hay comentarios:

Publicar un comentario