jueves, 21 de noviembre de 2013

ProRecocha

En este pequeño programa veremos la ejecución de una aplicación en la cual se verán unas imágenes en una lista y se mostraran en una pequeña ventana cada vez que se seleccione una, y por otro lado se creara un Boton dificil de darle click.




Esto sera posible por la ejecución de la siguiente lineas de códigos:

 private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String imagen="";
        imagen=jComboBox1.getSelectedItem().toString();
        ImageIcon icono=new ImageIcon(getClass().getResource("..\\paqimagenes\\"+imagen+".jpg"));
        Image imagen1=icono.getImage();
        ImageIcon icono_escalado=new ImageIcon(imagen1.getScaledInstance(316, 198, Image.SCALE_SMOOTH));
        jLabel1.setIcon(icono_escalado);
    }                                          

Se utilizara un Combo box al cual se le daran las instrucciones, se redirigira a la selección de imagenes que se encuentra en el paqimagenes, que se encuentra emnumeradas de 1 a 4, y un escalamiento para su visualizacion.


Ahora viene el Botón Escurridizo.

 private void jBtnRecochaMouseMoved(java.awt.event.MouseEvent evt) {                                       
        // TODO add your handling code here:
        Random r=new Random();
        Integer x;
        Integer y;
        x=r.nextInt(780);
        y=r.nextInt(520);
        jBtnRecocha.setBounds(x, y, 120, 80);
    }                                      

Aqui se le dara una posición aleatoria en el jframe asignandole una posicion en grados en la ventana.

ProValidar letras

En este ejercicio se validara un campo de texto o JTxtField para que solo el usuario pueda ingresar en el letras más no números u otro simbolo:




En el cual se utilizara el siguiente código:

Aqui en primero se utilizara el método KeyTyped para capturar el texto ingresado al campo de texto, se usara

if ((evt.getKeyChar()>='a' && evt.getKeyChar()<='z') || (evt.getKeyChar()>=65 && evt.getKeyChar()<=90))
        {
         
        }
        else
             evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);


miércoles, 20 de noviembre de 2013

Procedula validación

En este nuevo ejercicio retomamos el procedula que se hizo en un comienzo, para mi caso, tuve que volverlo a hacer, perdi el archivo oringinal.




TUTORIAL.



Para poder entender como es la validación de los campos de texto que maneja el formulario ProCedula, comenzaremos a desglosar los campos básicos, solo letras, solo números.



Para los campos que requieren solo nombres, como son los de Apellidos, Nombres, con la siguiente línea de códigos:


if ((evt.getKeyChar()>='a' && evt.getKeyChar()<='z') || (evt.getKeyChar()>=65 && evt.getKeyChar()<=90))
    {
      
     }
    else
        evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);


Para los campos que requieren validación de solo números se utiliza la siguiente línea de códigos:


private void jTxtcedulaKeyTyped(java.awt.event.KeyEvent evt) {                                    
        // TODO add your handling code here:
        if (evt.getKeyChar()>='0' && evt.getKeyChar()<= '9' || evt.getKeyChar()== '.')
       {
           
       }
       else
           evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);


Para el campo de la ciudad y departamentos usaremos la línea de códigos que se uso en el ejercicio de prociudad:

private void jCbxDepartamentosActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
       jCbxCiudades.removeAllItems();
        if (jCbxDepartamentos.getSelectedItem().equals("Tolima"))
     {
         jCbxCiudades.addItem("Armero Guayabal");//agrega items   
         jCbxCiudades.addItem("Honda");
         jCbxCiudades.addItem("Mariquita");
         jCbxCiudades.addItem("Prado");
     }
     
     if (jCbxDepartamentos.getSelectedItem().equals("Cundinamarca"))
     {
         jCbxCiudades.addItem("Bogota");//agrega items   
         jCbxCiudades.addItem("Guaduas");
         jCbxCiudades.addItem("Soacha");
         jCbxCiudades.addItem("Villeta");
     }
     if (jCbxDepartamentos.getSelectedItem().equals("Caldas"))
     {
         jCbxCiudades.addItem("Victoria");//agrega items   
         jCbxCiudades.addItem("La Dorada");
         jCbxCiudades.addItem("Samana");
         jCbxCiudades.addItem("La Merced");
     }
      if (jCbxDepartamentos.getSelectedItem().equals("Antioquia" ))
              {   
         jCbxCiudades.addItem("Medellin");//agrega items   
         jCbxCiudades.addItem("Bello");
         jCbxCiudades.addItem("Itagui");
         jCbxCiudades.addItem("Marinilla");
         jCbxCiudades.addItem("Puerto Berrio");
         
        
         
    }
    }                                                 

Para la validación del Grupo sanguineo y El RH se usaran las dos siguientes líneas de código:

GS
private void jTxtGSKeyTyped(java.awt.event.KeyEvent evt) {                                
        // TODO add your handling code here:
    if (evt.getKeyChar()=='A' || evt.getKeyChar()== 'B' || evt.getKeyChar()== 'O')
        {
           
        }
        else

            evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);    


RH:

private void jTxtRHKeyTyped(java.awt.event.KeyEvent evt) {                                
        // TODO add your handling code here:
   if (evt.getKeyChar()=='+' || evt.getKeyChar()== '-')
        {
           
        }
        else

            evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);

Para el campo del género de la persona se utilizara la siguiente linea de códigos:


private void jTxtSEXOKeyTyped(java.awt.event.KeyEvent evt) {                                  
        // TODO add your handling code here:
        if ((evt.getKeyChar()>='F' && evt.getKeyChar()<='M') || (evt.getKeyChar()>=70 && evt.getKeyChar()<=77))
    {
      
     }
    else
        evt.setKeyChar(KeyEvent.CHAR_UNDEFINED);

    }                                 

martes, 19 de noviembre de 2013

Propedro

Aqui simularemos la famosa aplicación creada unos años atras, y que dio mucho de que hablar entre los entusiastas de los sistemas, "Pedro Responde"





En el siguiente código se detalla el funcionamiento del programa, aqui utilizaremos el modo de capturar teclas en un jTxtField o campo de texto por el metodo de KeyTyped, donde se guardaran las palabras que el usuario ingresara:

String cadena="Pedro por favor Responde esta pregunta";
    Integer contador=0;
    String respuesta="";
    boolean punto=false;
    private void jTxtPreguntaKeyTyped(java.awt.event.KeyEvent evt) {                                    
        // TODO add your handling code here:
        if(evt.getKeyChar()=='.')
            punto=true;
       
        if(punto==false)
         respuesta=respuesta+evt.getKeyChar();
       
        if(contador<cadena.length())
        {
        evt.setKeyChar(cadena.charAt(contador));//esconde la respuesta
        contador++;
        }
       
    }                                    


y con esta última línea de código se mostrara en pantalla el resultado:

private void jTxtPreguntaKeyPressed(java.awt.event.KeyEvent evt) {                                        
        // TODO add your handling code here:
        if (evt.getKeyCode()==KeyEvent.VK_ENTER){
            JOptionPane.showMessageDialog(this, respuesta);
        this.dispose();


ProCiudades

En este ejercicio se realiza un programa por el cual nos va a permitir elegir por cada de departamento los diferentes municipios que cada uno contenga.




Se usa dos combo box, uno para departamento y otro para ciudad, para que cumpla su funcionalidad se utiliza el siguiente codigo:

jCbxCiudades.removeAllItems();
        if (jCbxDepartamentos.getSelectedItem().equals("Tolima"))
     {
         jCbxCiudades.addItem("Armero Guayabal");//agrega items  
         jCbxCiudades.addItem("Honda");
         jCbxCiudades.addItem("Mariquita");
         jCbxCiudades.addItem("Prado");
     }
   
     if (jCbxDepartamentos.getSelectedItem().equals("Cundinamarca"))
     {
         jCbxCiudades.addItem("Bogota");//agrega items  
         jCbxCiudades.addItem("Guaduas");
         jCbxCiudades.addItem("Soacha");
         jCbxCiudades.addItem("Villeta");
     }
      if (jCbxDepartamentos.getSelectedItem().equals("Antioquia" ))
              {  
         jCbxCiudades.addItem("Medellin");//agrega items  
         jCbxCiudades.addItem("Bello");
         jCbxCiudades.addItem("Itagui");
         jCbxCiudades.addItem("Rio Negro");
         jCbxCiudades.addItem("Barbosa");
       
       
       
    }

domingo, 29 de septiembre de 2013

Formulario Calculadora hecho en net beans



Aqui se puede ver el diseño de la calculadora en ejecución

Ejercicio Calculadora (Línea de códigos)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jCalculadora.java
 *
 * Created on 12/09/2013, 06:53:21 PM
 */
package paqCalculadora;

/**
 *
 * @author 554
 */
public class jCalculadora extends javax.swing.JFrame {

    /** Creates new form jCalculadora */
    public jCalculadora() {
        initComponents();
        this.setBounds(1, 1, 350, 300);
       
       
       
       
    }

    /** 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() {

        jBtnMC = new javax.swing.JButton();
        jBtnMR = new javax.swing.JButton();
        jBtnMS = new javax.swing.JButton();
        jBtnMmas = new javax.swing.JButton();
        jBtnMmenos = new javax.swing.JButton();
        jBtnMasMenos = new javax.swing.JButton();
        jBtnBorrar = new javax.swing.JButton();
        jBtnLimpiarTodo = new javax.swing.JButton();
        jBtnLimpiar = new javax.swing.JButton();
        jBtnRaiz = new javax.swing.JButton();
        jBtnOcho = new javax.swing.JButton();
        jBtnsiete = new javax.swing.JButton();
        jBtnNueve = new javax.swing.JButton();
        jBtnDivision = new javax.swing.JButton();
        jBtnPorcentaje = new javax.swing.JButton();
        jBtnCuatro = new javax.swing.JButton();
        jBtnCinco = new javax.swing.JButton();
        jBtnSeis = new javax.swing.JButton();
        jBtnPor = new javax.swing.JButton();
        jBtnCociente = new javax.swing.JButton();
        jBtnUno = new javax.swing.JButton();
        jBtnDos = new javax.swing.JButton();
        jBtnTres = new javax.swing.JButton();
        jBtnMenos = new javax.swing.JButton();
        jBtnMas = new javax.swing.JButton();
        jBtnIgual = new javax.swing.JButton();
        jBtnDecimal = new javax.swing.JButton();
        jBtnCero = new javax.swing.JButton();
        jTxtPantalla = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBtnMC.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMC.setText("MC");
        jBtnMC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMCActionPerformed(evt);
            }
        });

        jBtnMR.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMR.setText("MR");

        jBtnMS.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMS.setText("MS");

        jBtnMmas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMmas.setText("M+");

        jBtnMmenos.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMmenos.setText("M-");

        jBtnMasMenos.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMasMenos.setText("±");

        jBtnBorrar.setFont(new java.awt.Font("Arial", 1, 11)); // NOI18N
        jBtnBorrar.setText("?");

        jBtnLimpiarTodo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnLimpiarTodo.setText("CE");

        jBtnLimpiar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnLimpiar.setText("C");

        jBtnRaiz.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnRaiz.setText("v");

        jBtnOcho.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnOcho.setText("8");
        jBtnOcho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnOchoActionPerformed(evt);
            }
        });

        jBtnsiete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnsiete.setText("7");
        jBtnsiete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnsieteActionPerformed(evt);
            }
        });

        jBtnNueve.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnNueve.setText("9");
        jBtnNueve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNueveActionPerformed(evt);
            }
        });

        jBtnDivision.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnDivision.setText("/");
        jBtnDivision.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDivisionActionPerformed(evt);
            }
        });

        jBtnPorcentaje.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnPorcentaje.setText("%");

        jBtnCuatro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnCuatro.setText("4");
        jBtnCuatro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCuatroActionPerformed(evt);
            }
        });

        jBtnCinco.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnCinco.setText("5");
        jBtnCinco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCincoActionPerformed(evt);
            }
        });

        jBtnSeis.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnSeis.setText("6");
        jBtnSeis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSeisActionPerformed(evt);
            }
        });

        jBtnPor.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnPor.setText("*");
        jBtnPor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPorActionPerformed(evt);
            }
        });

        jBtnCociente.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnCociente.setText("1/x");

        jBtnUno.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnUno.setText("1");
        jBtnUno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnUnoActionPerformed(evt);
            }
        });

        jBtnDos.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnDos.setText("2");
        jBtnDos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDosActionPerformed(evt);
            }
        });

        jBtnTres.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnTres.setText("3");
        jBtnTres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnTresActionPerformed(evt);
            }
        });

        jBtnMenos.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMenos.setText("-");
        jBtnMenos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMenosActionPerformed(evt);
            }
        });

        jBtnMas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnMas.setText("+");
        jBtnMas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMasActionPerformed(evt);
            }
        });

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

        jBtnDecimal.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnDecimal.setText(",");
        jBtnDecimal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDecimalActionPerformed(evt);
            }
        });

        jBtnCero.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jBtnCero.setText("0");
        jBtnCero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCeroActionPerformed(evt);
            }
        });

        jTxtPantalla.setBackground(new java.awt.Color(255, 255, 255));
        jTxtPantalla.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jTxtPantalla.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jTxtPantalla.setText("0");
        jTxtPantalla.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jTxtPantalla.setOpaque(true);

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTxtPantalla, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnMC, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMR, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMS, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMmas, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMmenos, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnLimpiarTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMasMenos, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnRaiz, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnsiete, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnOcho, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnNueve, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnDivision, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnPorcentaje, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnCuatro, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnCinco, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnSeis, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnPor, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnCociente, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnUno, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(jBtnDos, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jBtnCero, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBtnTres, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBtnDecimal, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBtnMenos, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBtnMas, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addComponent(jBtnIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTxtPantalla, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnMC)
                    .addComponent(jBtnMR)
                    .addComponent(jBtnMS)
                    .addComponent(jBtnMmas)
                    .addComponent(jBtnMmenos))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnBorrar)
                    .addComponent(jBtnLimpiarTodo)
                    .addComponent(jBtnLimpiar)
                    .addComponent(jBtnMasMenos)
                    .addComponent(jBtnRaiz))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnsiete)
                    .addComponent(jBtnOcho)
                    .addComponent(jBtnNueve)
                    .addComponent(jBtnDivision)
                    .addComponent(jBtnPorcentaje))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBtnCuatro)
                    .addComponent(jBtnCinco)
                    .addComponent(jBtnSeis)
                    .addComponent(jBtnPor)
                    .addComponent(jBtnCociente))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBtnUno)
                            .addComponent(jBtnDos))
                        .addGap(6, 6, 6)
                        .addComponent(jBtnCero))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnTres)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnDecimal))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnMenos)
                        .addGap(6, 6, 6)
                        .addComponent(jBtnMas))
                    .addComponent(jBtnIgual, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

   boolean primero=true;//bandera (señal para el programa si es que se ejecuta por primer vez el programa)
   String operador1="";
   String operador2="";
    private void jBtnUnoActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
       
    if(mas==false){
        if(primero==true){
           jTxtPantalla.setText("1");//borra el cero
           primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+ "1");
        }
    }
    else
    {
        operador2=operador2+"1";
        jTxtPantalla.setText("<html><P ALIGN=right>"+operador1+" "+operador+"<P ALIGN=right>"+ operador2+"</html>");
    }
    }                                      

    private void jBtnDosActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        if(mas==false){
        if(primero==true){
           jTxtPantalla.setText("2");//borra el cero
           primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+ "2");
        }
    }
    else
    {
        operador2=operador2+"2";
        jTxtPantalla.setText("<html><P ALIGN=right>"+operador1+" "+operador+"<P ALIGN=right>"+ operador2+"</html>");
    }
    }                                      

    private void jBtnTresActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if (primero==true){
            jTxtPantalla.setText("3");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"3");
        }
    }                                       

    private void jBtnCuatroActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        if (primero==true){
            jTxtPantalla.setText("4");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"4");
        }
    }                                         

    private void jBtnCincoActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (primero==true){
            jTxtPantalla.setText("5");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"5");
        }
    }                                        

    private void jBtnSeisActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if (primero==true){
            jTxtPantalla.setText("6");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"6");
        }
    }                                       

    private void jBtnsieteActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if(primero==true){
            jTxtPantalla.setText("7");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"7");
        }   
    }                                        

    private void jBtnOchoActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if(primero==true){
            jTxtPantalla.setText("8");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"8");
        }
    }                                       

    private void jBtnNueveActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if(primero==true){
            jTxtPantalla.setText("9");
            primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+"9");
        }
    }                                        
     boolean mas=false;//bandera para saber si ya se presiono el mas
    private void jBtnMasActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        operador="+";
        operador1=jTxtPantalla.getText();
        jTxtPantalla.setText("<html>"+operador1+" +<br>"+operador1+"</html>" );
        mas=true;
       
    }                                      
        String operador="";//le va adecir al igual que operacion hacer
    private void jBtnIgualActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        Integer resultado=0;
        Double res_div=0.0;
        String resultado1="";
        if ("+".equals(operador))
        {
        resultado=Integer.parseInt(operador1)+Integer.parseInt(operador2);
        resultado1=resultado+"";
        }
        if ("-".equals(operador))
        {
        resultado=Integer.parseInt(operador1)-Integer.parseInt(operador2);
        resultado1=resultado+"";
        }
        if ("*".equals(operador))
        {
        resultado=Integer.parseInt(operador1)*Integer.parseInt(operador2);
        resultado1=resultado+"";
        }
        if ("/".equals(operador))
        {
          if(!operador2.equals("0"))
          {
          res_div=Double.parseDouble(operador1)/Double.parseDouble(operador2);
          Integer aux=0;
          aux=Integer.parseInt(operador1)/Integer.parseInt(operador2);
          if((res_div-aux)==0)
              resultado1=aux+"";
          else
          resultado1=res_div+"";
          }
          else 
          resultado1="No se puede dividir por 0";
         
        }
        jTxtPantalla.setText(resultado1);
       
        primero=true;
        mas=false;
        operador1=operador2="";
       
    }                                        

    private void jBtnMenosActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        operador="-";
        operador1=jTxtPantalla.getText();
        jTxtPantalla.setText("<html>"+operador1+" -<br>"+operador1+"</html>" );
        mas=true;
    }                                        

    private void jBtnPorActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        operador="*";
        operador1=jTxtPantalla.getText();
        jTxtPantalla.setText("<html>"+operador1+" *<br>"+operador1+"</html>");
        mas=true;
    }                                      

    private void jBtnDivisionActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        operador="/";
        operador1=jTxtPantalla.getText();
        jTxtPantalla.setText("<html>"+operador1+" /<br>"+operador1+"</html>");
        mas=true;
    }                                           

    private void jBtnCeroActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
         if(mas==false){
        if(primero==true){
           jTxtPantalla.setText("0");//borra el cero
           primero=false;
        }
        else
        {
            jTxtPantalla.setText(jTxtPantalla.getText()+ "0");
        }
    }
    else
    {
        operador2=operador2+"0";
        jTxtPantalla.setText("<html><P ALIGN=right>"+operador1+" "+operador+"<P ALIGN=right>"+ operador2+"</html>");
    }
    }                                       

    private void jBtnDecimalActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jBtnMCActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
    }                                     

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

            public void run() {
                new jCalculadora().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jBtnBorrar;
    private javax.swing.JButton jBtnCero;
    private javax.swing.JButton jBtnCinco;
    private javax.swing.JButton jBtnCociente;
    private javax.swing.JButton jBtnCuatro;
    private javax.swing.JButton jBtnDecimal;
    private javax.swing.JButton jBtnDivision;
    private javax.swing.JButton jBtnDos;
    private javax.swing.JButton jBtnIgual;
    private javax.swing.JButton jBtnLimpiar;
    private javax.swing.JButton jBtnLimpiarTodo;
    private javax.swing.JButton jBtnMC;
    private javax.swing.JButton jBtnMR;
    private javax.swing.JButton jBtnMS;
    private javax.swing.JButton jBtnMas;
    private javax.swing.JButton jBtnMasMenos;
    private javax.swing.JButton jBtnMenos;
    private javax.swing.JButton jBtnMmas;
    private javax.swing.JButton jBtnMmenos;
    private javax.swing.JButton jBtnNueve;
    private javax.swing.JButton jBtnOcho;
    private javax.swing.JButton jBtnPor;
    private javax.swing.JButton jBtnPorcentaje;
    private javax.swing.JButton jBtnRaiz;
    private javax.swing.JButton jBtnSeis;
    private javax.swing.JButton jBtnTres;
    private javax.swing.JButton jBtnUno;
    private javax.swing.JButton jBtnsiete;
    private javax.swing.JLabel jTxtPantalla;
    // End of variables declaration                  
}


jueves, 29 de agosto de 2013

Formulario Suma, Restar, Multiplicar y Dividir hecho en netbeans





Aqui se puede ver el programa realizando una operacion de suma (1+2=3)
Comprobando una de las operaciones matemáticas

Ejercicio Formulario Suma, Restar, Multiplicar y Dividir (Línea de Código)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paqSuma;

/**
 *
 * @author Diego Leonardo
 */
public class jSuma extends javax.swing.JFrame {

    /**
     * Creates new form jSuma
     */
    public jSuma() {
        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() {

        jTxtNumero1 = new javax.swing.JTextField();
        jTxtNumero2 = new javax.swing.JTextField();
        jBtnSumar = new javax.swing.JButton();
        jLblResultado = new javax.swing.JLabel();
        jBtnRestar = new javax.swing.JButton();
        jBtnMultiplicar = new javax.swing.JButton();
        jBtnDividir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBtnSumar.setText("Suma");
        jBtnSumar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSumarActionPerformed(evt);
            }
        });

        jLblResultado.setText("Resultado");

        jBtnRestar.setText("Resta");
        jBtnRestar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnRestarActionPerformed(evt);
            }
        });

        jBtnMultiplicar.setText("Multiplicacion");
        jBtnMultiplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMultiplicarActionPerformed(evt);
            }
        });

        jBtnDividir.setText("Division");
        jBtnDividir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDividirActionPerformed(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()
                .addContainerGap()
                .addComponent(jBtnSumar)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jLblResultado)
                        .addContainerGap(235, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jTxtNumero2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnRestar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jBtnMultiplicar)))
                        .addGap(18, 18, 18)
                        .addComponent(jBtnDividir)
                        .addGap(27, 27, 27))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jTxtNumero1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxtNumero1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxtNumero2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnSumar)
                    .addComponent(jBtnRestar)
                    .addComponent(jBtnMultiplicar)
                    .addComponent(jBtnDividir))
                .addGap(36, 36, 36)
                .addComponent(jLblResultado)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void jBtnSumarActionPerformed(java.awt.event.ActionEvent evt) {                                         
          // TODO add your handling code here:
          Integer numero1, numero2;
          numero1=Integer.parseInt(jTxtNumero1.getText()); //Asignacion
          //parseint es un metodo para convertir
          // de letras a números
          numero2=Integer.parseInt(jTxtNumero2.getText());
          Integer Resultado=0; //Inicializar una variable en cero
          Resultado=numero1+numero2;
          jLblResultado.setText("Resultado: "+Resultado);
      
    }                                        

    private void jBtnRestarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        Integer numero1, numero2;
          numero1=Integer.parseInt(jTxtNumero1.getText()); //Asignacion
          //parseint es un metodo para convertir
          // de letras a números
          numero2=Integer.parseInt(jTxtNumero2.getText());
          Integer Resultado=0; //Inicializar una variable en cero
          Resultado=numero1-numero2;
          jLblResultado.setText("Resultado: "+Resultado);
    }                                         

    private void jBtnMultiplicarActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        Integer numero1, numero2;
          numero1=Integer.parseInt(jTxtNumero1.getText()); //Asignacion
          //parseint es un metodo para convertir
          // de letras a números
          numero2=Integer.parseInt(jTxtNumero2.getText());
          Integer Resultado=0; //Inicializar una variable en cero
          Resultado=numero1*numero2;
          jLblResultado.setText("Resultado: "+Resultado);
    }                                              

    private void jBtnDividirActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
          Integer numero1, numero2;
          numero1=Integer.parseInt(jTxtNumero1.getText()); //Asignacion
          //parseint es un metodo para convertir
          // de letras a números
          numero2=Integer.parseInt(jTxtNumero2.getText());
          Integer Resultado=0; //Inicializar una variable en cero
          Resultado=numero1/numero2;
          jLblResultado.setText("Resultado: "+Resultado);
    }                                          

    /**
     * @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(jSuma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jSuma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jSuma.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jSuma.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 jSuma().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jBtnDividir;
    private javax.swing.JButton jBtnMultiplicar;
    private javax.swing.JButton jBtnRestar;
    private javax.swing.JButton jBtnSumar;
    private javax.swing.JLabel jLblResultado;
    private javax.swing.JTextField jTxtNumero1;
    private javax.swing.JTextField jTxtNumero2;
    // End of variables declaration                  
}