Buscar

10LDP_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

Você também pode ser Premium ajudando estudantes

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ê também pode ser Premium ajudando estudantes

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ê também pode ser Premium ajudando estudantes
Você viu 3, do total de 5 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

Você também pode ser Premium ajudando estudantes

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ê também pode ser Premium ajudando estudantes

Prévia do material em texto

Construindo as classes de tela de cadastro 
 
Vamos construir dois painéis para apresentar as diferenças no cadastro. O 
JPanelCorpoAtividade e o JPanelCorpoSocio. Em cada um dos painéis, vamos criar métodos 
para podermos acessar os atributos deste painel. Vamos renomear os campos JTextField para 
ficar mais amigável: 
 edt_atv 
 edt_desc 
Os métodos de acesso aos atributos são: 
 
 public void setAtv(int num){ 
 edt_atv.setText(Integer.toString(num)); 
 } 
 
 public void setNome(String texto){ 
 edt_desc.setText(texto); 
 } 
 
 public int getAtv(){ 
 return Integer.parseInt(edt_atv.getText()); 
 } 
 
 public String getNome(){ 
 return edt_desc.getText(); 
 } 
 
Observe que como o CodAtv é um Inteiro, se faz necessário converter Inteiro em String e vice 
versa. Utilizamos os métodos Integer.parseInt e o Integer.toString para efetuar as conversões. 
O painel do sócio é análogo. Construa da mesma maneira. 
Com os painéis prontos, vamos construir a classe TelaCad, que é uma classe abstrata para 
cadastros genéricos. 
 
/* 
 * TelaCad.java 
 * 
 */ 
 
package Visao; 
 
import javax.swing.JPanel; 
 
/** 
 * 
 * @author Eduardo Pareto 
 */ 
public abstract class TelaCad extends javax.swing.JFrame { 
 
 /** Creates new form TelaCad */ 
 public TelaCad() { 
 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. 
 */ 
 // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
 
 
 private void initComponents() { 
 
 jTitulo = new javax.swing.JLabel(); 
 jPainel = new javax.swing.JPanel(); 
 jbtnInclui = new javax.swing.JButton(); 
 jbtnConsulta = new javax.swing.JButton(); 
 jbtnAltera = new javax.swing.JButton(); 
 jbtnExclui = new javax.swing.JButton(); 
 jbtnSai = new javax.swing.JButton(); 
 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
 jTitulo.setFont(new java.awt.Font("Tahoma", 1, 14)); 
 jTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
 jPainel.setLayout(new java.awt.BorderLayout()); 
 jbtnInclui.setText("Incluir"); 
 jbtnInclui.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jbtnIncluiActionPerformed(evt); 
 } 
 }); 
 jbtnConsulta.setText("Consultar"); 
 jbtnConsulta.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jbtnConsultaActionPerformed(evt); 
 } 
 }); 
 jbtnAltera.setText("Alterar"); 
 jbtnAltera.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jbtnAlteraActionPerformed(evt); 
 } 
 }); 
 jbtnExclui.setText("Excluir"); 
 jbtnExclui.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jbtnExcluiActionPerformed(evt); 
 } 
 }); 
 jbtnSai.setText("Sair"); 
 jbtnSai.addActionListener(new java.awt.event.ActionListener() { 
 public void actionPerformed(java.awt.event.ActionEvent evt) { 
 jbtnSaiActionPerformed(evt); 
 } 
 }); 
 org.jdesktop.layout.GroupLayout layout = new 
org.jdesktop.layout.GroupLayout(getContentPane()); 
 getContentPane().setLayout(layout); 
 layout.setHorizontalGroup( 
 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
 .add(layout.createSequentialGroup() 
 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
 .add(layout.createSequentialGroup() 
 .add(105, 105, 105) 
 .add(jTitulo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 204, 
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
 .add(layout.createSequentialGroup() 
 .addContainerGap() 
 .add(jbtnInclui) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
 
 
 .add(jbtnConsulta) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
 .add(jbtnAltera) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
 .add(jbtnExclui) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
 .add(jbtnSai)) 
 .add(layout.createSequentialGroup() 
 .add(29, 29, 29) 
 .add(jPainel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 356, 
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) 
 .addContainerGap(34, Short.MAX_VALUE)) ); 
 layout.setVerticalGroup( 
 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
 .add(layout.createSequentialGroup() 
 .add(jTitulo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, 
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 
 .add(jPainel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 233, 
Short.MAX_VALUE) 
 .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) 
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 
 .add(jbtnConsulta) 
 .add(jbtnAltera) 
 .add(jbtnExclui) 
 .add(jbtnSai) 
 .add(jbtnInclui)) 
 .addContainerGap()) ); 
 
 pack(); 
 }// </editor-fold> 
 
 private void jbtnAlteraActionPerformed(java.awt.event.ActionEvent evt) { 
 trataAltera(); 
} 
 
 private void jbtnConsultaActionPerformed(java.awt.event.ActionEvent evt) { 
 trataConsulta(); 
} 
 
 private void jbtnIncluiActionPerformed(java.awt.event.ActionEvent evt) { 
 trataInclui(); 
 } 
 
 private void jbtnSaiActionPerformed(java.awt.event.ActionEvent evt) { 
 trataSai(); 
 } 
 
 private void jbtnExcluiActionPerformed(java.awt.event.ActionEvent evt) { 
 trataExclui(); 
 } 
 
 
 // Variables declaration - do not modify 
 private javax.swing.JPanel jPainel; 
 private javax.swing.JLabel jTitulo; 
 private javax.swing.JButton jbtnAltera; 
 private javax.swing.JButton jbtnConsulta; 
 
 
 private javax.swing.JButton jbtnExclui; 
 private javax.swing.JButtonjbtnInclui; 
 private javax.swing.JButton jbtnSai; 
 // End of variables declaration 
 
 abstract void trataAltera(); 
 
 abstract void trataConsulta(); 
 
 abstract void trataExclui(); 
 
 abstract void trataInclui(); 
 
 public void setCorpo(JPanel area){ 
 jPainel.add(area); 
 } 
 
 public void setTitulo(String texto){ 
 jTitulo.setText(texto); 
 } 
 
 private void trataSai() { 
 this.dispose(); 
 } 
 
} 
 
Esta tela de cadastro contem 5 botões e um painel. No painel genérico colocaremos os painéis 
específicos criado acima. Nos botões, programaremos genericamente as chamadas, para que 
nas heranças, estas programações sejam feitas corretamente. 
 
Para integrar as telas, a TelaPrinc precisa estar Associada a classe TelaCad. 
 TelaCad tela; 
O Objeto tela será instanciado como TelaCadClube ou TelaCadSocio, dependendo da opção. 
Neste momento, estamos implementando o Polomorfismo. Os métodos abaixo fazem os 
tratamentos dos eventos da barra de menu. 
 private void trataAtividade() { 
 tela = new TelaCadClube(); 
 tela.setVisible(true); 
 } 
 
 private void trataSocio() { 
 tela = new TelaCadSocio(); 
 tela.setVisible(true); 
 } 
 
Neste momento, nosso programa já tem a seguinte aparência:

Outros materiais

Materiais relacionados

Perguntas relacionadas

Perguntas Recentes