Advertisement
LeoSchimidt

Cadastro com txt

Mar 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JTextField;
  11.  
  12. public class Cadastro extends javax.swing.JFrame {
  13.  
  14.    
  15.    
  16.    
  17.     public Cadastro() {
  18.         initComponents();
  19.     }
  20.  
  21.     /**
  22.      * This method is called from within the constructor to initialize the form.
  23.      * WARNING: Do NOT modify this code. The content of this method is always
  24.      * regenerated by the Form Editor.
  25.      */
  26.     @SuppressWarnings("unchecked")
  27.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  28.     private void initComponents() {
  29.  
  30.         jPanel1 = new javax.swing.JPanel();
  31.         textCodigo = new javax.swing.JLabel();
  32.         txtCodigo = new javax.swing.JTextField();
  33.         textNome = new javax.swing.JLabel();
  34.         txtNome = new javax.swing.JTextField();
  35.         jLabel1 = new javax.swing.JLabel();
  36.         txtCPF = new javax.swing.JTextField();
  37.         textTelefone = new javax.swing.JLabel();
  38.         txtTelefone = new javax.swing.JTextField();
  39.         textLogradouro = new javax.swing.JLabel();
  40.         txtLogradouro = new javax.swing.JTextField();
  41.         textNumero = new javax.swing.JLabel();
  42.         txtNumero = new javax.swing.JTextField();
  43.         textBairro = new javax.swing.JLabel();
  44.         txtBairro = new javax.swing.JTextField();
  45.         textCidade = new javax.swing.JLabel();
  46.         txtCidade = new javax.swing.JTextField();
  47.         textEstado = new javax.swing.JLabel();
  48.         txtEstado = new javax.swing.JTextField();
  49.         btnBuscar = new javax.swing.JButton();
  50.         btnLimpar = new javax.swing.JButton();
  51.         btnInserir = new javax.swing.JButton();
  52.  
  53.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  54.         setTitle("Cadastro com Arquivo txt");
  55.  
  56.         textCodigo.setText("Código:");
  57.  
  58.         txtCodigo.addActionListener(new java.awt.event.ActionListener() {
  59.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  60.                 txtCodigoActionPerformed(evt);
  61.             }
  62.         });
  63.  
  64.         textNome.setText("Nome:");
  65.         textNome.setMaximumSize(new java.awt.Dimension(37, 14));
  66.         textNome.setMinimumSize(new java.awt.Dimension(37, 14));
  67.  
  68.         jLabel1.setText("CPF:");
  69.  
  70.         textTelefone.setText("Telefone:");
  71.  
  72.         textLogradouro.setText("Logradouro:");
  73.  
  74.         textNumero.setText("Nº:");
  75.  
  76.         textBairro.setText("Bairro:");
  77.  
  78.         txtBairro.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 txtBairroActionPerformed(evt);
  81.             }
  82.         });
  83.  
  84.         textCidade.setText("Cidade:");
  85.  
  86.         textEstado.setText("Estado:");
  87.  
  88.         btnBuscar.setText("Buscar");
  89.         btnBuscar.addActionListener(new java.awt.event.ActionListener() {
  90.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  91.                 btnBuscarActionPerformed(evt);
  92.             }
  93.         });
  94.  
  95.         btnLimpar.setText("Limpar");
  96.         btnLimpar.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 btnLimparActionPerformed(evt);
  99.             }
  100.         });
  101.  
  102.         btnInserir.setText("Inserir");
  103.         btnInserir.addActionListener(new java.awt.event.ActionListener() {
  104.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  105.                 btnInserirActionPerformed(evt);
  106.             }
  107.         });
  108.  
  109.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  110.         jPanel1.setLayout(jPanel1Layout);
  111.         jPanel1Layout.setHorizontalGroup(
  112.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113.             .addGroup(jPanel1Layout.createSequentialGroup()
  114.                 .addGap(29, 29, 29)
  115.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  116.                     .addGroup(jPanel1Layout.createSequentialGroup()
  117.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  118.                             .addGroup(jPanel1Layout.createSequentialGroup()
  119.                                 .addComponent(textLogradouro)
  120.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  121.                                 .addComponent(txtLogradouro))
  122.                             .addGroup(jPanel1Layout.createSequentialGroup()
  123.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  124.                                     .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  125.                                     .addComponent(textNome, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  126.                                     .addComponent(textCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  127.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  128.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  129.                                     .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
  130.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  131.                                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  132.                                             .addComponent(txtCPF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
  133.                                             .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
  134.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  135.                                         .addComponent(textTelefone)))))
  136.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138.                             .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
  139.                             .addGroup(jPanel1Layout.createSequentialGroup()
  140.                                 .addComponent(textNumero)
  141.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  142.                                 .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))))
  143.                     .addGroup(jPanel1Layout.createSequentialGroup()
  144.                         .addGap(63, 63, 63)
  145.                         .addComponent(textBairro)
  146.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  147.                         .addComponent(txtBairro))
  148.                     .addGroup(jPanel1Layout.createSequentialGroup()
  149.                         .addComponent(textCidade)
  150.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  151.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  152.                             .addGroup(jPanel1Layout.createSequentialGroup()
  153.                                 .addComponent(btnBuscar)
  154.                                 .addGap(68, 68, 68)
  155.                                 .addComponent(btnLimpar)
  156.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  157.                                 .addComponent(btnInserir))
  158.                             .addGroup(jPanel1Layout.createSequentialGroup()
  159.                                 .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                                 .addGap(41, 41, 41)
  161.                                 .addComponent(textEstado)
  162.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  163.                                 .addComponent(txtEstado)))))
  164.                 .addGap(46, 46, 46))
  165.         );
  166.         jPanel1Layout.setVerticalGroup(
  167.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.             .addGroup(jPanel1Layout.createSequentialGroup()
  169.                 .addGap(34, 34, 34)
  170.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  171.                     .addComponent(textCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
  172.                     .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  173.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  174.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  175.                     .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
  176.                     .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  177.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  178.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  179.                     .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
  180.                     .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  181.                     .addComponent(textTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
  182.                     .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  183.                 .addGap(31, 31, 31)
  184.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  185.                     .addComponent(textLogradouro)
  186.                     .addComponent(txtLogradouro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  187.                     .addComponent(textNumero)
  188.                     .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  189.                 .addGap(18, 18, 18)
  190.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  191.                     .addComponent(textBairro)
  192.                     .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  193.                 .addGap(18, 18, 18)
  194.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  195.                     .addComponent(textCidade)
  196.                     .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  197.                     .addComponent(textEstado)
  198.                     .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  199.                 .addGap(18, 18, 18)
  200.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  201.                     .addComponent(btnBuscar)
  202.                     .addComponent(btnLimpar)
  203.                     .addComponent(btnInserir))
  204.                 .addContainerGap(20, Short.MAX_VALUE))
  205.         );
  206.  
  207.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  208.         getContentPane().setLayout(layout);
  209.         layout.setHorizontalGroup(
  210.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  211.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  212.         );
  213.         layout.setVerticalGroup(
  214.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  215.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  216.         );
  217.  
  218.         pack();
  219.     }// </editor-fold>                        
  220.  
  221.     private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {                                          
  222.         // TODO add your handling code here:
  223.     }                                        
  224.  
  225.     private void txtBairroActionPerformed(java.awt.event.ActionEvent evt) {                                          
  226.         // TODO add your handling code here:
  227.     }                                        
  228.  
  229.     private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {                                          
  230.  
  231.         txtCodigo.setText("");
  232.         txtNome.setText("");
  233.         txtCPF.setText("");
  234.         txtTelefone.setText("");
  235.         txtLogradouro.setText("");
  236.         txtNumero.setText("");
  237.         txtBairro.setText("");
  238.         txtCidade.setText("");
  239.         txtEstado.setText("");
  240.  
  241.         // TODO add your handling code here:
  242.     }                                        
  243.  
  244.     private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  245.        
  246.         try{
  247.                 String arquivo = JOptionPane.showInputDialog(null, "Informe o código do dado a ser consultado.");
  248.                
  249.                 BufferedReader br = new BufferedReader(new FileReader(arquivo + ".txt"));
  250.                
  251.                 PrintWriter out = new PrintWriter(txtCodigo.getText() + ".txt");
  252.                
  253.                 out.println(txtCodigo.getText());
  254.                 out.println(txtNome.getText());
  255.                 out.println(txtCPF.getText());
  256.                 out.println(txtTelefone.getText());
  257.                 out.println(txtLogradouro.getText());
  258.                 out.println(txtNumero.getText());
  259.                 out.println(txtCidade.getText());
  260.                 out.println(txtEstado.getText());
  261.                
  262.                 JOptionPane.showMessageDialog(null, "Dados gravados com sucesso.");
  263.            
  264.             }catch (IOException Erro){
  265.                
  266.                 JOptionPane.showMessageDialog(null, "Ocorreu algum erro ao cadastrar os dados.");
  267.            
  268.             }
  269.  
  270.  
  271.  
  272.         // TODO add your handling code here:
  273.     }                                        
  274.  
  275.     private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {                                          
  276.  
  277.         if(txtCodigo.getText().equals("")){
  278.             JOptionPane.showMessageDialog(null, "O campo código não pode estar em branco");
  279.             txtCodigo.requestFocus();
  280.         }
  281.        
  282.         else if(txtNome.getText().equals("")){
  283.             JOptionPane.showMessageDialog(null, "O campo nome não pode estar em branco");
  284.             txtNome.requestFocus();
  285.         }
  286.        
  287.         else if(txtCPF.getText().equals("")){
  288.             JOptionPane.showMessageDialog(null, "O campo CPF não pode estar em branco");
  289.             txtCPF.requestFocus();
  290.         }
  291.        
  292.         else if(txtTelefone.getText().equals("")){
  293.             JOptionPane.showMessageDialog(null, "O campo telefone não pode estar em branco");
  294.             txtTelefone.requestFocus();
  295.         }
  296.        
  297.         else if(txtLogradouro.getText().equals("")){
  298.             JOptionPane.showMessageDialog(null, "O campo logradouro não pode estar em branco");
  299.             txtLogradouro.requestFocus();
  300.         }
  301.        
  302.         else if(txtNumero.getText().equals("")){
  303.             JOptionPane.showMessageDialog(null, "O campo número não pode estar em branco");
  304.             txtNumero.requestFocus();
  305.         }
  306.        
  307.         else if(txtBairro.getText().equals("")){
  308.             JOptionPane.showMessageDialog(null, "O campo bairro não pode estar em branco");
  309.             txtBairro.requestFocus();
  310.         }
  311.        
  312.         else if(txtCidade.getText().equals("")){
  313.             JOptionPane.showMessageDialog(null, "O campo cidade não pode estar em branco");
  314.             txtCidade.requestFocus();
  315.         }
  316.        
  317.         else if(txtEstado.getText().equals("")){
  318.             JOptionPane.showMessageDialog(null, "O campo estado não pode estar em branco");
  319.             txtEstado.requestFocus();
  320.         }
  321.        
  322.        
  323.        
  324.     }                                          
  325.  
  326.     /**
  327.      * @param args the command line arguments
  328.      */
  329.     public static void main(String args[]) {
  330.        
  331.        
  332.        
  333.        
  334.         /* Set the Nimbus look and feel */
  335.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  336.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  337.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  338.          */
  339.         try {
  340.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  341.                 if ("Nimbus".equals(info.getName())) {
  342.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  343.                     break;
  344.                 }
  345.             }
  346.         } catch (ClassNotFoundException ex) {
  347.             java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  348.         } catch (InstantiationException ex) {
  349.             java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  350.         } catch (IllegalAccessException ex) {
  351.             java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  352.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  353.             java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  354.         }
  355.         //</editor-fold>
  356.  
  357.         /* Create and display the form */
  358.         java.awt.EventQueue.invokeLater(new Runnable() {
  359.             public void run() {
  360.                 new Cadastro().setVisible(true);
  361.             }
  362.         });
  363.     }
  364.  
  365.     // Variables declaration - do not modify                    
  366.     private javax.swing.JButton btnBuscar;
  367.     private javax.swing.JButton btnInserir;
  368.     private javax.swing.JButton btnLimpar;
  369.     private javax.swing.JLabel jLabel1;
  370.     private javax.swing.JPanel jPanel1;
  371.     private javax.swing.JLabel textBairro;
  372.     private javax.swing.JLabel textCidade;
  373.     private javax.swing.JLabel textCodigo;
  374.     private javax.swing.JLabel textEstado;
  375.     private javax.swing.JLabel textLogradouro;
  376.     private javax.swing.JLabel textNome;
  377.     private javax.swing.JLabel textNumero;
  378.     private javax.swing.JLabel textTelefone;
  379.     private javax.swing.JTextField txtBairro;
  380.     private javax.swing.JTextField txtCPF;
  381.     private javax.swing.JTextField txtCidade;
  382.     private javax.swing.JTextField txtCodigo;
  383.     private javax.swing.JTextField txtEstado;
  384.     private javax.swing.JTextField txtLogradouro;
  385.     private javax.swing.JTextField txtNome;
  386.     private javax.swing.JTextField txtNumero;
  387.     private javax.swing.JTextField txtTelefone;
  388.     // End of variables declaration                  
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement