Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package proyectoprestamos; import java.io.IOException; import java.text.DateFormat; import java.util.Calendar; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import org.apache.commons.validator.routines.CreditCardValidator; /** * * @author Sergio */ public class DatosUsuario extends javax.swing.JFrame { String numeroDni; /** * Creates new form DatosUsuario */ public DatosUsuario() { initComponents(); Calendar cal = Calendar.getInstance(); DateFormat formateadorFechaLarga = DateFormat.getDateInstance(DateFormat.MEDIUM); System.out.println(formateadorFechaLarga.format(cal.getTime())); jLabelFecha2.setText(String.valueOf(formateadorFechaLarga.format(cal.getTime()))); DateFormat formateadorHoraCorta = DateFormat.getTimeInstance(DateFormat.SHORT); System.out.println(formateadorHoraCorta.format(cal.getTime())); jLabelHora2.setText(String.valueOf(formateadorHoraCorta.format(cal.getTime()))); setLocationRelativeTo(null); setIconImage(new ImageIcon(getClass().getResource("/imagenes/p.png")).getImage()); } /** * 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() { jLabel11 = new javax.swing.JLabel(); nombreUsuario = new javax.swing.JTextField(); jLabel12 = new javax.swing.JLabel(); apellidosUsuario = new javax.swing.JTextField(); jLabel13 = new javax.swing.JLabel(); dniUsuario = new javax.swing.JTextField(); jLabel14 = new javax.swing.JLabel(); tarjetaCredito = new javax.swing.JTextField(); Enviar = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabelFecha2 = new javax.swing.JLabel(); jLabelHora2 = new javax.swing.JLabel(); jButtonValidar = new javax.swing.JButton(); jButtonPagarPaypal = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jDateChooser1 = new com.toedter.calendar.JDateChooser(); jLabel4 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel11.setText("Nombre:"); nombreUsuario.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { nombreUsuarioActionPerformed(evt); } }); jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel12.setText("Apellidos:"); apellidosUsuario.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { apellidosUsuarioActionPerformed(evt); } }); jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel13.setText("Dni:"); dniUsuario.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dniUsuarioActionPerformed(evt); } }); jLabel14.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel14.setText("tarjeta de credito:"); tarjetaCredito.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tarjetaCreditoActionPerformed(evt); } }); Enviar.setBackground(new java.awt.Color(0, 0, 0)); Enviar.setForeground(new java.awt.Color(239, 159, 93)); Enviar.setText("Enviar"); Enviar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { EnviarActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("Datos del Usuario"); jLabel2.setText("-"); jButton1.setBackground(new java.awt.Color(0, 0, 0)); jButton1.setForeground(new java.awt.Color(239, 159, 93)); jButton1.setText("Atrs"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabelFecha2.setBackground(new java.awt.Color(255, 153, 0)); jLabelFecha2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N jLabelFecha2.setText("jLabel3"); jLabelHora2.setBackground(new java.awt.Color(255, 153, 0)); jLabelHora2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N jLabelHora2.setText("jLabel3"); jButtonValidar.setText("Validar"); jButtonValidar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonValidarActionPerformed(evt); } }); jButtonPagarPaypal.setText("Recibir prestamo por Paypal"); jButtonPagarPaypal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonPagarPaypalActionPerformed(evt); } }); jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/paypal.jpg"))); // NOI18N jLabel4.setText("Vlido hasta:"); 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(130, 130, 130).addComponent(jLabel1)) .addGroup(layout.createSequentialGroup().addGap(160, 160, 160).addComponent(jLabel3)) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel14) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButtonPagarPaypal).addComponent(tarjetaCredito, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18).addComponent(jButtonValidar))) .addGap(0, 166, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addGap(47, 47, 47).addComponent(Enviar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1).addGap(93, 93, 93)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel11) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(nombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabelHora2)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel13).addComponent(jLabel12)) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(4, 4, 4) .addComponent(dniUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26).addComponent(jLabel4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(apellidosUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabelFecha2))) .addGap(49, 49, 49))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel11) .addComponent(nombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel12).addComponent(apellidosUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(1, 1, 1).addComponent(jLabelFecha2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabelHora2))) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel13) .addComponent(dniUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel14) .addComponent(tarjetaCredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonValidar)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonPagarPaypal) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Enviar).addComponent(jButton1)) .addGap(26, 26, 26))); pack(); }// </editor-fold>//GEN-END:initComponents private void nombreUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreUsuarioActionPerformed // TODO add your handling code here: }//GEN-LAST:event_nombreUsuarioActionPerformed private void apellidosUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apellidosUsuarioActionPerformed // TODO add your handling code here: }//GEN-LAST:event_apellidosUsuarioActionPerformed private void dniUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dniUsuarioActionPerformed // TODO add your handling code here: }//GEN-LAST:event_dniUsuarioActionPerformed private void EnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EnviarActionPerformed numeroDni = dniUsuario.getText(); if (esDni(numeroDni)) { int respuesta = JOptionPane.showConfirmDialog(null, "El prstamo se ha realizado correctamente", "Gracias", JOptionPane.CLOSED_OPTION); } else { JOptionPane.showMessageDialog(this, "El Dni introducido no es vlido, debe tener 8 dgitos", "Error", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_EnviarActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed this.setVisible(false); }//GEN-LAST:event_jButton1ActionPerformed private void tarjetaCreditoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tarjetaCreditoActionPerformed // TODO add your handling code here: }//GEN-LAST:event_tarjetaCreditoActionPerformed private void jButtonValidarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonValidarActionPerformed CreditCardValidator ccValidator = new CreditCardValidator(); boolean numeroTarjeta = ccValidator.isValid(tarjetaCredito.getText()); String mensaje = numeroTarjeta ? "Tarjeta validada" : "Tarjeta Incorrecta"; JOptionPane.showMessageDialog(this, mensaje); }//GEN-LAST:event_jButtonValidarActionPerformed private void jButtonPagarPaypalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPagarPaypalActionPerformed //ejecutar un comando para que me abra google chrome y la pagina de paypal try { Process proceso = Runtime.getRuntime().exec( "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe https://www.paypal.com/es/home"); } catch (IOException e) { e.printStackTrace(); } }//GEN-LAST:event_jButtonPagarPaypalActionPerformed /** * @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(DatosUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DatosUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DatosUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DatosUsuario.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 DatosUsuario().setVisible(true); } }); //Prestamos Prestamos = new Prestamos(); // totalPrestamo.setText(String.valueOf(prestamon)); } public static boolean esDni(String dni) { Pattern dniPattern = Pattern.compile("(\\d{8,8})"); Matcher m = dniPattern.matcher(dni); if (m.matches()) { return true; } else return false; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Enviar; private javax.swing.JTextField apellidosUsuario; private javax.swing.JTextField dniUsuario; private javax.swing.JButton jButton1; private javax.swing.JButton jButtonPagarPaypal; private javax.swing.JButton jButtonValidar; private com.toedter.calendar.JDateChooser jDateChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabelFecha2; private javax.swing.JLabel jLabelHora2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField nombreUsuario; private javax.swing.JTextField tarjetaCredito; // End of variables declaration//GEN-END:variables }