Buscar

14POO aula05 doc02

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 3, do total de 10 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 6, do total de 10 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 9, do total de 10 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Prévia do material em texto

Abaixo, segue o código do programa. 
Classe Pessoa: 
package Modelo; 
 
/** 
 * 
 * @author Eduardo 
 */ 
public class Pessoa { 
 private String nome; 
 private String email; 
 private boolean sexo; 
 
 /** 
 * @return the nome 
 */ 
 public String getNome() { 
 return nome; 
 } 
 
 /** 
 * @param nome the nome to set 
 */ 
 public void setNome(String nome) { 
 this.nome = nome; 
 } 
 
 /** 
 * @return the email 
 */ 
 public String getEmail() { 
 return email; 
 } 
 
 /** 
 * @param email the email to set 
 */ 
 public void setEmail(String email) { 
 this.email = email; 
 } 
 /** 
 * @return the sexo 
 */ 
 public boolean isSexo() { 
 return sexo; 
 } 
 /** 
 * @param sexo the sexo to set 
 
 
 */ 
 public void setSexo(boolean sexo) { 
 this.sexo = sexo; 
 } 
} 
 
Classe CadastroPessoa 
 
package projaula5pooead; 
 
import Modelo.Pessoa; 
import java.awt.Color; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
/** 
 * 
 * @author Eduardo 
 */ 
public class CadastroPessoa extends javax.swing.JFrame { 
 
 List pessoas; 
 Iterator i; 
 /** Creates new form CadastroPessoa */ 
 public CadastroPessoa() { 
 initComponents(); 
 pessoas = new ArrayList(); 
 } 
 
 /** 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() { 
 
 buttonGroup1 = new javax.swing.ButtonGroup(); 
 jLabel1 = new javax.swing.JLabel(); 
 jTFNome = new javax.swing.JTextField(); 
 jRBMasc = new javax.swing.JRadioButton(); 
 jRBFem = new javax.swing.JRadioButton(); 
 jLabel2 = new javax.swing.JLabel(); 
 jLabel3 = new javax.swing.JLabel(); 
 
 
 jTFEmail = new javax.swing.JTextField(); 
 jLblValida = new javax.swing.JLabel(); 
 jBtInclui = new javax.swing.JButton(); 
 jBtLimpa = new javax.swing.JButton(); 
 jBtAnterior = new javax.swing.JButton(); 
 jBtProximo = new javax.swing.JButton(); 
 jCBHabilita = new javax.swing.JCheckBox(); 
 jLblMsg = new javax.swing.JLabel(); 
 
 
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
 setTitle("Exercício Cadastro"); 
 
 jLabel1.setText("Nome:"); 
 
 jTFNome.addKeyListener(new java.awt.event.KeyAdapter() { 
 public void keyTyped(java.awt.event.KeyEvent evt) { 
 trataTeclaApertada(evt); 
 } 
 }); 
 
 buttonGroup1.add(jRBMasc); 
 jRBMasc.setSelected(true); 
 jRBMasc.setText("Masculino"); 
 
 buttonGroup1.add(jRBFem); 
 jRBFem.setText("Feminino"); 
 
 jLabel2.setText("Sexo:"); 
 
 jLabel3.setText("email:"); 
 
 jTFEmail.addFocusListener(new java.awt.event.FocusAdapter() { 
 public void focusLost(java.awt.event.FocusEvent evt) { 
 verificaEmail(evt); 
 } 
 }); 
 jTFEmail.addKeyListener(new java.awt.event.KeyAdapter() { 
 public void keyTyped(java.awt.event.KeyEvent evt) { 
 trataTeclaApertada(evt); 
 } 
 }); 
 
 jBtInclui.setText("Inclui"); 
 jBtInclui.setEnabled(false); 
 jBtInclui.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 trataInclui(evt); 
 
 
 } 
 }); 
 
 jBtLimpa.setText("Limpa"); 
 jBtLimpa.setEnabled(false); 
 jBtLimpa.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 trataLimpa(evt); 
 } 
 }); 
 
 jBtAnterior.setText("Início"); 
 jBtAnterior.setEnabled(false); 
 jBtAnterior.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 trataInicio(evt); 
 } 
 }); 
 
 jBtProximo.setText("Próximo"); 
 jBtProximo.setEnabled(false); 
 jBtProximo.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 trataProximo(evt); 
 } 
 }); 
 
 jCBHabilita.setText("Habilita Navegação"); 
 jCBHabilita.addItemListener(new java.awt.event.ItemListener() { 
 public void itemStateChanged(java.awt.event.ItemEvent evt) { 
 trataMudancaEstado(evt); 
 } 
 }); 
 
 jLblMsg.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N 
 jLblMsg.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
 
 javax.swing.GroupLayout layout = new 
javax.swing.GroupLayout(getContentPane()); 
 getContentPane().setLayout(layout); 
 layout.setHorizontalGroup( 
 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(2, 2, 2) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRA
ILING) 
 
 
 .addComponent(jLblMsg, 
javax.swing.GroupLayout.PREFERRED_SIZE, 254, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(2, 2, 2) 
 .addComponent(jCBHabilita) 
 .addGap(18, 18, 18) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING) 
 .addGroup(layout.createSequentialGroup() 
 .addComponent(jBtInclui) 
 .addGap(18, 18, 18) 
 .addComponent(jBtLimpa)) 
 .addGroup(layout.createSequentialGroup() 
 .addComponent(jBtAnterior) 
 .addGap(49, 49, 49) 
 .addComponent(jBtProximo)))) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(85, 85, 85) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRA
ILING) 
 .addComponent(jLabel3) 
 .addComponent(jLabel1) 
 .addComponent(jLabel2)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING, false).addComponent(jRBFem) 
 .addComponent(jRBMasc) 
 .addComponent(jTFNome, 
javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE) 
 .addComponent(jTFEmail)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
 .addComponent(jLblValida, 
javax.swing.GroupLayout.PREFERRED_SIZE, 86, 
javax.swing.GroupLayout.PREFERRED_SIZE)))) 
 .addGap(50, 50, 50)) 
 ); 
 layout.setVerticalGroup( 
 
 
 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
 .addGroup(layout.createSequentialGroup() 
 .addGap(57, 57, 57) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAS
ELINE) 
 .addComponent(jLabel1) 
 .addComponent(jTFNome, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAS
ELINE) 
 .addComponent(jRBMasc) 
 .addComponent(jLabel2)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
 .addComponent(jRBFem) 
 .addGap(8, 8, 8) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAS
ELINE) 
 .addComponent(jLabel3) 
 .addComponent(jTFEmail, 
javax.swing.GroupLayout.PREFERRED_SIZE, 
javax.swing.GroupLayout.DEFAULT_SIZE, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 .addComponent(jLblValida, 
javax.swing.GroupLayout.PREFERRED_SIZE, 20, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 
45, Short.MAX_VALUE) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAS
ELINE) 
 .addComponent(jBtInclui) 
 .addComponent(jBtLimpa)) 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
 .addComponent(jLblMsg, 
javax.swing.GroupLayout.PREFERRED_SIZE, 20, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
 
 
 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BAS
ELINE) 
 .addComponent(jBtProximo) 
 .addComponent(jCBHabilita) 
 .addComponent(jBtAnterior)) 
 .addGap(14, 14, 14)) 
 ); 
 
 pack(); 
 }// </editor-fold> 
 
 private void trataInclui(java.awt.event.ActionEvent evt) { 
 Pessoa p = new Pessoa(); 
 p.setNome(jTFNome.getText()); 
 p.setEmail(jTFEmail.getText()); 
 p.setSexo(jRBMasc.isSelected()); 
 pessoas.add(p); 
 limpaCampos(); 
 jTFNome.requestFocus(); 
 i=pessoas.iterator(); 
 
 } 
 
 private void trataLimpa(java.awt.event.ActionEvent evt) { 
 limpaCampos(); 
 } 
 
 private void trataMudancaEstado(java.awt.event.ItemEvent evt) { 
 Object[] obj = jCBHabilita.getSelectedObjects(); 
 if(obj!=null){ 
 habilitaInclusao(true); 
 i=pessoas.iterator(); 
 mostraElemento(); 
 } 
 
 else 
 habilitaInclusao(false); 
 } 
 
 private void trataTeclaApertada(java.awt.event.KeyEvent evt) { 
 jBtLimpa.setEnabled(true); 
 } 
 
 private void verificaEmail(java.awt.event.FocusEvent evt) { 
 String email = jTFEmail.getText(); 
 
 
 Color c = new Color(0,255,0); 
 if(validaEmail(email)){ 
 c=Color.BLUE; 
 jLblValida.setText("Válido"); 
 jBtInclui.setEnabled(true); 
 jBtInclui.requestFocus(); 
 } 
 else{ 
 c=Color.RED; 
 jLblValida.setText("Email Inválido"); 
 if(jTFEmail.getText().equals("")) 
 jLblValida.setText(""); 
 else 
 jTFEmail.requestFocus(); 
 
 } 
 jLblValida.setForeground(c); 
 
 } 
 
 private void trataInicio(java.awt.event.ActionEvent evt) { 
 i=pessoas.iterator(); 
 jBtProximo.setEnabled(true); 
 jLblMsg.setText(""); 
 mostraElemento(); 
 } 
 
 private void trataProximo(java.awt.event.ActionEvent evt) { 
 mostraElemento(); 
 } 
 
 private boolean validaEmail(String email){ 
 //Define o padrão da entrada de dados 
 Pattern p = Pattern.compile(".+@.+\\.[a-z]+"); 
 //Valida a cadeia de caracteres com o padrão definido 
 Matcher m = p.matcher(email); 
 return (m.matches()); 
 
 } 
 
 private void habilitaInclusao(boolean b) { 
 
 jBtAnterior.setEnabled(b); 
 jBtProximo.setEnabled(b); 
 jBtInclui.setEnabled(!b); 
 jBtLimpa.setEnabled(!b); 
 limpaCampos(); 
 } 
 
 
 
 private void limpaCampos(){ 
 jTFNome.setText(""); 
 jTFEmail.setText(""); 
 jLblValida.setText(""); 
 jBtLimpa.setEnabled(false); 
 jBtInclui.setEnabled(false); 
 jRBMasc.setSelected(true); 
 } 
 
 private void mostraElemento() { 
 Pessoa auxPessoa; 
 if(i.hasNext()){ 
 auxPessoa = (Pessoa) i.next(); 
 jTFNome.setText(auxPessoa.getNome()); 
 jTFEmail.setText(auxPessoa.getEmail()); 
 if(auxPessoa.isSexo()) 
 jRBMasc.setSelected(true); 
 else 
 jRBFem.setSelected(true); 
 } 
 else { 
 jLblMsg.setText("Não existe mais elemento"); 
 jBtProximo.setEnabled(false); 
 } 
 } 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(String args[]) { 
 java.awt.EventQueue.invokeLater(new Runnable() { 
 public void run() { 
 new CadastroPessoa().setVisible(true); 
 } 
 }); 
 } 
 
 // Variables declaration - do not modify 
 private javax.swing.ButtonGroup buttonGroup1; 
 private javax.swing.JButton jBtAnterior; 
 private javax.swing.JButton jBtInclui; 
 private javax.swing.JButton jBtLimpa; 
 private javax.swing.JButton jBtProximo; 
 private javax.swing.JCheckBox jCBHabilita; 
 private javax.swing.JLabel jLabel1; 
 private javax.swing.JLabel jLabel2; 
 private javax.swing.JLabel jLabel3; 
 private javax.swing.JLabel jLblMsg; 
 
 
 private javax.swing.JLabel jLblValida; 
 private javax.swing.JRadioButton jRBFem; 
 private javax.swing.JRadioButtonjRBMasc; 
 private javax.swing.JTextField jTFEmail; 
 private javax.swing.JTextField jTFNome; 
 // End of variables declaration 
}

Outros materiais