[SOLUCIONADO] Necesito un java GUI, que permita 3 Jtextfield, nombre,rut...

Iniciado por pancho992, Junio 06, 2013, 08:38:07 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Junio 06, 2013, 08:38:07 PM Ultima modificación: Agosto 30, 2014, 09:26:54 AM por Expermicid
Hola, soy estudiante y de verdad este trabajo me tiene ya con las meas ojeras xD ... si alguien se apiada de mi ... le estare muy agradecido (Y)

pd : tengo hecho una validador de rut .. y deberia guardarlos en un archivo.txt ... ese es mi drama la vez no cacho como guardar lo que ingreso
una vez que hago click en el boton ...  y me falta poder los otros 2 jtextfield con nombre y apellido los que tambien tiene que ir guardados en el .txt

Profavor ayudenme :) !!

Código: java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ValidarRut   extends JFrame implements ActionListener {

    private JButton btnValidar;
    private JTextField txtRut;
    private JLabel labRut;

    public ValidarRut()
    {
    super  ( " Validación de RUT" );
        this.setSize(400, 200);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       

        // Agregamos elementos a la ventana
        this.setLayout( new FlowLayout(FlowLayout.LEFT, 20, 10)  );

        labRut = new JLabel(" Rut");
        txtRut = new JTextField(30);
        txtRut.setText(" ");
        btnValidar = new JButton("Precione Para Validar");
        btnValidar.addActionListener(this);
        this.add(labRut);
        this.add(txtRut);
        this.add(btnValidar);

        this.setVisible(true);

    }

    public static void main(String[] args) {
        // TODO code application logic here
        new ValidarRut();
    }

    public void actionPerformed(ActionEvent e) {

        // Validamos el RUt
        if (e.getSource()==btnValidar)
        {
            if ( txtRut.getText().length() > 0  )
            {
                // Creamos un arreglo con el rut y el digito verificador
                String[] rut_dv = txtRut.getText().replace(".","").split("-");
                // Las partes del rut (numero y dv) deben tener una longitud positiva
                if ( rut_dv.length == 2   )
                {
                    // Capturamos error (al convertir un string a entero)
                    try
                    {
                        int rut = Integer.parseInt( rut_dv[0] );
                        char dv = rut_dv[1].charAt(0);

                        // Validamos que sea un rut valido según la norma
                        if ( this.ValidarRut(rut, dv)  )
                        {
                            JOptionPane.showMessageDialog(rootPane, "Rut correcto");
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(rootPane, "Rut incorrecto");
                        }
                    }
                    catch( Exception ex )
                    {
                        System.out.println(" Error " + ex.getMessage());
                    }
                }
            }
        }
    }

    /*
     * Método Estático que valida si un rut es válido
     * Fuente : http://www.creations.cl/2009/01/generador-de-rut-y-validador/
     */
    public static boolean ValidarRut(int rut, char dv)
    {
   
        int m = 0, s = 1;
        for (; rut != 0; rut /= 10)
        {
            s = (s + rut % 10 * (9 - m++ % 6)) % 11;
        }
        return dv == (char) (s != 0 ? s + 47 : 75 );
    }

}

Código: java
package Control;

import java.util.regex.*;

public class Validaciones {

public static String mensaje;

public static boolean rut(String rut)
{
boolean tiro = false;
Pattern rut1 = Pattern.compile("^\\d{1,2}+.\\d{3}+.\\d{3}[-][0-9kK]{1}$");
if(!rut1.matcher(rut).find())
mensaje = "El RUT debe tener el siguiente formato: xx.xxx.xxx-x";
else
{
String dv = rut.substring(rut.length()-1, rut.length()).toUpperCase();
rut = rut.substring(0,rut.length()-2);
String rut_limpio = rut.replace(".", "");

int rut_ver = Integer.parseInt(rut_limpio);

int Digito;
     int Contador;
     int Multiplo;
     int Acumulador;
     String rutDigito;

     Contador = 2;
     Acumulador = 0;

     while (rut_ver != 0)
     {
      Multiplo = (rut_ver % 10) * Contador;
      Acumulador = Acumulador + Multiplo;
      rut_ver = rut_ver/10;
      Contador = Contador + 1;
      if (Contador == 8)
      {
          Contador = 2;
      }
      }

      Digito = 11 - (Acumulador % 11);
      rutDigito = Integer.toString(Digito);
     
      if (Digito == 10 )
      {
         rutDigito = "K";
      }
      else if (Digito == 11)
      {
            rutDigito = "0";
      }
      if(rutDigito.equals(dv))
          tiro = true;
      else
      mensaje = "Digito Verificador incorrecto.";
}

return tiro;
}

public static boolean soloLetras(String cadena)
{
if(Pattern.matches("[A-Za-z]+", cadena))
return true;
else
{
mensaje = "Sólo se aceptan letras para el campo ";
return false;
}
}
}


jojojo saludos hermano xD
Tengo una pregunta que a veces me tortura "estoy loco yo o los locos son los demás"