Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- public class Cadastro extends javax.swing.JFrame {
- public Cadastro() {
- 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() {
- jPanel1 = new javax.swing.JPanel();
- textCodigo = new javax.swing.JLabel();
- txtCodigo = new javax.swing.JTextField();
- textNome = new javax.swing.JLabel();
- txtNome = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- txtCPF = new javax.swing.JTextField();
- textTelefone = new javax.swing.JLabel();
- txtTelefone = new javax.swing.JTextField();
- textLogradouro = new javax.swing.JLabel();
- txtLogradouro = new javax.swing.JTextField();
- textNumero = new javax.swing.JLabel();
- txtNumero = new javax.swing.JTextField();
- textBairro = new javax.swing.JLabel();
- txtBairro = new javax.swing.JTextField();
- textCidade = new javax.swing.JLabel();
- txtCidade = new javax.swing.JTextField();
- textEstado = new javax.swing.JLabel();
- txtEstado = new javax.swing.JTextField();
- btnBuscar = new javax.swing.JButton();
- btnLimpar = new javax.swing.JButton();
- btnInserir = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Cadastro com Arquivo txt");
- textCodigo.setText("Código:");
- txtCodigo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtCodigoActionPerformed(evt);
- }
- });
- textNome.setText("Nome:");
- textNome.setMaximumSize(new java.awt.Dimension(37, 14));
- textNome.setMinimumSize(new java.awt.Dimension(37, 14));
- jLabel1.setText("CPF:");
- textTelefone.setText("Telefone:");
- textLogradouro.setText("Logradouro:");
- textNumero.setText("Nº:");
- textBairro.setText("Bairro:");
- txtBairro.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtBairroActionPerformed(evt);
- }
- });
- textCidade.setText("Cidade:");
- textEstado.setText("Estado:");
- btnBuscar.setText("Buscar");
- btnBuscar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnBuscarActionPerformed(evt);
- }
- });
- btnLimpar.setText("Limpar");
- btnLimpar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnLimparActionPerformed(evt);
- }
- });
- btnInserir.setText("Inserir");
- btnInserir.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnInserirActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(29, 29, 29)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(textLogradouro)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(txtLogradouro))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(textNome, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(textCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(txtCPF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(textTelefone)))))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(textNumero)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(63, 63, 63)
- .addComponent(textBairro)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(txtBairro))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(textCidade)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(btnBuscar)
- .addGap(68, 68, 68)
- .addComponent(btnLimpar)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(btnInserir))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(41, 41, 41)
- .addComponent(textEstado)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(txtEstado)))))
- .addGap(46, 46, 46))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(34, 34, 34)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(textTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(31, 31, 31)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textLogradouro)
- .addComponent(txtLogradouro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(textNumero)
- .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textBairro)
- .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(textCidade)
- .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(textEstado)
- .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnBuscar)
- .addComponent(btnLimpar)
- .addComponent(btnInserir))
- .addContainerGap(20, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>
- private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void txtBairroActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {
- txtCodigo.setText("");
- txtNome.setText("");
- txtCPF.setText("");
- txtTelefone.setText("");
- txtLogradouro.setText("");
- txtNumero.setText("");
- txtBairro.setText("");
- txtCidade.setText("");
- txtEstado.setText("");
- // TODO add your handling code here:
- }
- private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- String arquivo = JOptionPane.showInputDialog(null, "Informe o código do dado a ser consultado.");
- BufferedReader br = new BufferedReader(new FileReader(arquivo + ".txt"));
- PrintWriter out = new PrintWriter(txtCodigo.getText() + ".txt");
- out.println(txtCodigo.getText());
- out.println(txtNome.getText());
- out.println(txtCPF.getText());
- out.println(txtTelefone.getText());
- out.println(txtLogradouro.getText());
- out.println(txtNumero.getText());
- out.println(txtCidade.getText());
- out.println(txtEstado.getText());
- JOptionPane.showMessageDialog(null, "Dados gravados com sucesso.");
- }catch (IOException Erro){
- JOptionPane.showMessageDialog(null, "Ocorreu algum erro ao cadastrar os dados.");
- }
- // TODO add your handling code here:
- }
- private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {
- if(txtCodigo.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo código não pode estar em branco");
- txtCodigo.requestFocus();
- }
- else if(txtNome.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo nome não pode estar em branco");
- txtNome.requestFocus();
- }
- else if(txtCPF.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo CPF não pode estar em branco");
- txtCPF.requestFocus();
- }
- else if(txtTelefone.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo telefone não pode estar em branco");
- txtTelefone.requestFocus();
- }
- else if(txtLogradouro.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo logradouro não pode estar em branco");
- txtLogradouro.requestFocus();
- }
- else if(txtNumero.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo número não pode estar em branco");
- txtNumero.requestFocus();
- }
- else if(txtBairro.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo bairro não pode estar em branco");
- txtBairro.requestFocus();
- }
- else if(txtCidade.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo cidade não pode estar em branco");
- txtCidade.requestFocus();
- }
- else if(txtEstado.getText().equals("")){
- JOptionPane.showMessageDialog(null, "O campo estado não pode estar em branco");
- txtEstado.requestFocus();
- }
- }
- /**
- * @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(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Cadastro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Cadastro.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 Cadastro().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnBuscar;
- private javax.swing.JButton btnInserir;
- private javax.swing.JButton btnLimpar;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JLabel textBairro;
- private javax.swing.JLabel textCidade;
- private javax.swing.JLabel textCodigo;
- private javax.swing.JLabel textEstado;
- private javax.swing.JLabel textLogradouro;
- private javax.swing.JLabel textNome;
- private javax.swing.JLabel textNumero;
- private javax.swing.JLabel textTelefone;
- private javax.swing.JTextField txtBairro;
- private javax.swing.JTextField txtCPF;
- private javax.swing.JTextField txtCidade;
- private javax.swing.JTextField txtCodigo;
- private javax.swing.JTextField txtEstado;
- private javax.swing.JTextField txtLogradouro;
- private javax.swing.JTextField txtNome;
- private javax.swing.JTextField txtNumero;
- private javax.swing.JTextField txtTelefone;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement