Como fazer set e get?

4 respostas
M
Ola pessoal... estou com o seguinte código de interface em anexo...e agora o professor disse q tenho q separar a parte de programçaõ em métodos get e set mas eu não seo fazer... alguém pode me ajudar?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class Projeto {
    
    public static void main(String[] args) {
        //Tela Programa Principal
        TelaPrincipal menu = new TelaPrincipal();
        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}

vou colocar todas as class...só nao vai ter as imagens

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TelaPrincipal extends JFrame implements ActionListener
{
	ImageIcon image1 = new ImageIcon("cliente.png");
	ImageIcon image2 = new ImageIcon("produto.png");
	ImageIcon image3 = new ImageIcon("servico.png");
	ImageIcon image4 = new ImageIcon("agenda.png");
	ImageIcon image5 = new ImageIcon("Sobre.png");
	private JButton bCliente = new JButton("clientes",image1);
    private JButton bProduto = new JButton("Produtos",image2);
    private JButton bServico = new JButton("Serviço",image3);
    private JButton bAgenda  = new JButton("Agenda",image4);
    private JButton bInfo  = new JButton("Sobre...",image5);
    
    private Container myPainel = getContentPane();
    public JPanel myJPanel = new JPanel();
    MyDesktopPane jdPane = new MyDesktopPane();//chamando a classe my descktop pane    

    TelasInternasC TelaCliente ;
    TelasInternasS TelaServico; 
    TelasInternasP TelaProduto;
    TelasInternasA TelaAgenda;
    TelasInternasI TelaInfo;
    
    public TelaPrincipal()
    {
    	myPainel.setLayout(new BorderLayout());
    	myPainel.add("North", myJPanel);
    	
    	getContentPane().setLayout(new BorderLayout());
    	JPanel painel = new JPanel();
    	getContentPane().add("North", painel);
    	painel.setLayout(new GridLayout(1, 4, 4, 4));
    	
    	
    	painel.add(bCliente);
        painel.add(bProduto);
        painel.add(bServico);
        painel.add(bAgenda);
        painel.add(bInfo);
        
        getContentPane().add(jdPane);
 
        
        
        bCliente.addActionListener(this);
        bProduto.addActionListener(this);
        bServico.addActionListener(this);
        bAgenda .addActionListener(this);
        bInfo.addActionListener(this);        
        setSize(800, 600);
        setVisible(true);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    	
    }
    public void actionPerformed(ActionEvent evt)
    {
    	if (evt.getSource() == bCliente) {
            if ((TelaCliente == null)||(TelaCliente.isValid()==false))
            {
                TelaCliente = new TelasInternasC("Cadastro de Clientes", this);
                                       
           }
            jdPane.moveToFront(TelaCliente);
            
        }
        
        if (evt.getSource() == bProduto) {
            if ((TelaProduto == null)||(TelaProduto.isValid()==false))
            {
                TelaProduto = new TelasInternasP("Cadastro de Produtos", this);
                
            }
            jdPane.moveToFront(TelaProduto);
        }
      	if (evt.getSource() == bServico) {
            if ((TelaServico == null)||(TelaServico.isValid()==false))
            {
                TelaServico = new TelasInternasS("Cadastro de Servicos", this);
            }
            jdPane.moveToFront(TelaServico);
        }
        if (evt.getSource() == bAgenda) {
            if ((TelaAgenda == null)||(TelaAgenda.isValid()==false))
            {
                TelaAgenda = new TelasInternasA("Agenda ", this);
            }
            jdPane.moveToFront(TelaAgenda);
        }
        
        if (evt.getSource() == bInfo) {
            if ((TelaInfo == null)||(TelaInfo.isValid()==false))
            {
                TelaInfo = new TelasInternasI("Sobre...", this);
            }
            jdPane.moveToFront(TelaInfo);
        }
    }
}
import java.awt.*;
import javax.swing.*;



public class TelasInternasA extends JInternalFrame 
{
	 public JLabel  lblData,  lblHora, lblServico,lblAnotacao , lblCliente;
	 public JTextField jtfData,  jtfHora;
	 public JTextArea jtaAnotacao;
	 public JComboBox jcbCliente;
	 public JComboBox jcbServico;
     public JButton jbBotaoOK,  jbBotaoCancelar, jbBotaoListar;
     	
     	
     public TelaPrincipal telaPrincipal;
     
     
     public TelasInternasA(String titulo, TelaPrincipal telaPrincipal) 
     {
         super(titulo, true ,true, true, true);
     
       
        lblCliente    = new JLabel("Cliente");
        jcbCliente    = new JComboBox();
        
        
        jcbCliente.addItem("weslley");
        jcbCliente.addItem("mailson");
        jcbCliente.addItem("jhonatan");
        jcbCliente.addItem("diogenes");
        
        
        
        jtfData       = new JTextField();
        jtfHora       = new JTextField();
        jcbServico    = new JComboBox();
        
        lblData       = new JLabel("Data:");
        lblHora       = new JLabel("Hora:");
        lblServico    = new JLabel("Serviços:");
        
        
        
        
        jcbServico.addItem("corte");
        jcbServico.addItem("pintura");
        jcbServico.addItem("penteado");
        jcbServico.addItem("maquiagem");
        
        
        lblAnotacao   = new JLabel("Anotação:");
               
       
        JPanel pGrid = new JPanel(new GridLayout(5, 1000000));
        
       
        pGrid.add(lblCliente);
        pGrid.add(jcbCliente);
        pGrid.add(lblData);
        pGrid.add(jtfData);
        pGrid.add(lblHora);
        pGrid.add(jtfHora);
        pGrid.add(lblServico);
        pGrid.add(jcbServico);
        pGrid.add(lblAnotacao);
        
        
        jtaAnotacao = new JTextArea();
        
        JScrollPane jspAnotacao = new JScrollPane(jtaAnotacao);
        
        jbBotaoOK = new JButton("CONFIRMAR");
        jbBotaoCancelar = new JButton("CANCELAR");
        jbBotaoListar = new JButton("LISTAR");
        
        JPanel pBotoes = new JPanel(new GridLayout(0, 3));
        pBotoes.add(jbBotaoOK);
        pBotoes.add(jbBotaoCancelar);
        pBotoes.add(jbBotaoListar);
        
        this.telaPrincipal = telaPrincipal;
        telaPrincipal.jdPane.add(this);
        
        
        this.setLayout(new BorderLayout());
        this.getContentPane().add(pGrid, BorderLayout.NORTH);
        this.getContentPane().add(jspAnotacao, BorderLayout.CENTER);
        this.getContentPane().add(pBotoes, BorderLayout.SOUTH);
        
        setSize(500 ,300);
        setVisible(true);
        

     }

}
import java.awt.*;
import javax.swing.*;



public class TelasInternasC extends JInternalFrame 
{
	 public JLabel lblNome,  lblEndereco,  lblTelefone, lblEmail, lblLinha;
	 public JTextField jtfNome,  jtfEndereco,  jtfTelefone ,jtfEmail;
     public JButton jbBotaoOK,  jbBotaoCancelar, jbBotaoListar;
     
     
     public TelaPrincipal telaPrincipal;
     
     
     public TelasInternasC(String titulo, TelaPrincipal telaPrincipal) 
     {
         super(titulo, true, true, true, true);
     
     
     
        jtfNome      = new JTextField();//campo
        jtfEndereco  = new JTextField();
        jtfTelefone  = new JTextField();
        jtfEmail     = new JTextField();
        
        lblLinha     = new JLabel("");
        lblNome      = new JLabel("Nome:");//nome
        lblEndereco  = new JLabel("Endereço:");
        lblTelefone  = new JLabel("Telefone:");
        lblEmail     = new JLabel("Email:");
        
      
             
        JPanel pGrid = new JPanel(new GridLayout(4, 1000000));
        
        pGrid.add(lblNome);
        pGrid.add(jtfNome);
        pGrid.add(lblEndereco);
        pGrid.add(jtfEndereco);
        pGrid.add(lblTelefone);
        pGrid.add(jtfTelefone);
        pGrid.add(lblEmail);
        pGrid.add(jtfEmail);
        
        
        
                
                
        jbBotaoOK = new JButton("CONFIRMAR");
        jbBotaoCancelar = new JButton("CANCELAR");
        jbBotaoListar = new JButton("LISTAR");
        
        JPanel pBotoes = new JPanel(new GridLayout(0, 3));
        pBotoes.add(jbBotaoOK);
        pBotoes.add(jbBotaoCancelar);
        pBotoes.add(jbBotaoListar);
        
        this.telaPrincipal = telaPrincipal;
        telaPrincipal.jdPane.add(this);
        
        
        this.setLayout(new BorderLayout());
        this.getContentPane().add(pGrid, BorderLayout.NORTH);
        this.getContentPane().add(pBotoes, BorderLayout.SOUTH);
        
        setSize(500 ,300);
        setVisible(true);

     }

}
import java.awt.*;
import javax.swing.*;



public class TelasInternasP extends JInternalFrame 
{
	 public JLabel lblNome,  lblCodigo,  lblQuantidade, lblDescricao;
	 public JTextField jtfNome,  jtfCodigo,  jtfQuantidade;
	 public JTextArea jtaDescricao;
     public JButton jbBotaoOK,  jbBotaoCancelar, jbBotaoListar;
     
     
     public TelaPrincipal telaPrincipal;
     
     
     public TelasInternasP(String titulo, TelaPrincipal telaPrincipal) 
     {
         super(titulo, true, true, true, true);
     
     
        jtfQuantidade  = new JTextField(10);      
        jtfCodigo      = new JTextField(10);
        jtfNome        = new JTextField(50);
        
        lblNome       = new JLabel("Nome:");
        lblCodigo     = new JLabel("Código:");
        lblQuantidade = new JLabel("Quantidade:");
        lblDescricao  = new JLabel("Descrição:");
        
        

        
        
        JPanel pGrid = new JPanel(new GridLayout(4, 1000000));
        
        pGrid.add(lblNome);
        pGrid.add(jtfNome);
        pGrid.add(lblCodigo);
        pGrid.add(jtfCodigo);
        pGrid.add(lblQuantidade);
        pGrid.add(jtfQuantidade);
        pGrid.add(lblDescricao);
        
        jtaDescricao = new JTextArea();
        
        JScrollPane jspDescricao = new JScrollPane(jtaDescricao);
        
        jbBotaoOK = new JButton("CONFIRMAR");
        jbBotaoCancelar = new JButton("CANCELAR");
        jbBotaoListar = new JButton("LISTAR");
        
        JPanel pBotoes = new JPanel(new GridLayout(0, 3));
        pBotoes.add(jbBotaoOK);
        pBotoes.add(jbBotaoCancelar);
        pBotoes.add(jbBotaoListar);
        
        this.telaPrincipal = telaPrincipal;
        telaPrincipal.jdPane.add(this);
        
        
        this.setLayout(new BorderLayout());
        this.getContentPane().add(pGrid, BorderLayout.NORTH);
        this.getContentPane().add(jspDescricao, BorderLayout.CENTER);
        this.getContentPane().add(pBotoes, BorderLayout.SOUTH);
        
        setSize(500 ,300);
        setVisible(true);
     }

}
import java.awt.*;
import javax.swing.*;

				

public class TelasInternasS extends JInternalFrame 
{
	 public JLabel lblNome,  lblCodigo,  lblValor, lblDescricao;
	 public JTextField jtfNome,  jtfCodigo,  jtfValor;
	 public JTextArea jtaDescricao;
     public JButton jbBotaoOK,  jbBotaoCancelar, jbBotaoListar;
     
     
     public TelaPrincipal telaPrincipal;
     
     
     public TelasInternasS(String titulo, TelaPrincipal telaPrincipal) 
     {
         super(titulo , true, true, true, true);
         
     
        jtfNome       = new JTextField();
        jtfCodigo     = new JTextField();
        jtfValor      = new JTextField();
        
        lblNome       = new JLabel("Nome:");
        lblCodigo     = new JLabel("Codigo:");
        lblValor      = new JLabel("Valor:");
        lblDescricao  = new JLabel("Descrissao:");
        
        JPanel pGrid = new JPanel(new GridLayout(4, 1000000));
        
        pGrid.add(lblNome);
        pGrid.add(jtfNome);
        pGrid.add(lblCodigo);
        pGrid.add(jtfCodigo);
        pGrid.add(lblValor);
        pGrid.add(jtfValor);
        pGrid.add(lblDescricao);
        
        jtaDescricao = new JTextArea();
        
        JScrollPane jspDescricao = new JScrollPane(jtaDescricao);
        
        jbBotaoOK = new JButton("CONFIRMAR");
        jbBotaoCancelar = new JButton("CANCELAR");
        jbBotaoListar = new JButton("LISTAR");
        
        JPanel pBotoes = new JPanel(new GridLayout(0, 3));
        pBotoes.add(jbBotaoOK);
        pBotoes.add(jbBotaoCancelar);
        pBotoes.add(jbBotaoListar);
        
        this.telaPrincipal = telaPrincipal;
        telaPrincipal.jdPane.add(this);
        
        
        this.setLayout(new BorderLayout());
        this.getContentPane().add(pGrid, BorderLayout.NORTH);
        this.getContentPane().add(jspDescricao, BorderLayout.CENTER);
        this.getContentPane().add(pBotoes, BorderLayout.SOUTH);
        
        setSize(500 ,300);
        setVisible(true);

     }

}
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JDesktopPane;

class MyDesktopPane extends JDesktopPane
{
  Image img;
  public MyDesktopPane()
  {
    try
    {
      img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("logo.png"), "logo.png"));
    }
    catch(Exception e){}
  }
    @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this);
  }
}
import java.awt.*;
import javax.swing.*;



public class TelasInternasI extends JInternalFrame 
{
	 
     public TelaPrincipal telaPrincipal;
     
       Image img;
     
     public TelasInternasI(String titulo, TelaPrincipal telaPrincipal) 
     {
         super(titulo, true, true, true);
        
    try
    {
      img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("sobre.jpg"), "sobre.jpg"));
    }
    catch(Exception e){}
         this.telaPrincipal = telaPrincipal;
         telaPrincipal.jdPane.add(this);
        
         this.setLayout(new BorderLayout());
         setSize(500 ,300);
         setVisible(true);
         

     }
     
    @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this);
  }

}

quem puder me ajudar como fazer os métodos set e get separados das class eu agradeço...email
[email removido]

4 Respostas

berg.pb

Tu bom?

Os get/set são, nada mais, do que métodos de acesso aos atributos presentes nas classes.
Como os atributos devem permanecer, geralmente, privados para poder diminuir o acoplamento entre os objetos, será através dos métodos get/set que vc irá obter/alterar os valores destes atributos.

Exmplo:
public class Pessoa{
  //conjunto de atributos configurados para serem privados.
   private String nome;
   private int idade;
   private double salario;

   //construtor default da aplicação
   public Pessoa(){}

   //métodos de acesso aos atributos. para cada atributo, um get e set.
   public void setNome(String nome){
     this.nome = nome;
   }

   public String getNome(){
     return nome;
   }

   //aqui vc continua fazendo o exemplo
}

Só umas observações:
* geralmente, qdo vc usa o metodo SET, vc tem q passar algum parâmetro para alterar. O retorno dele é quase sempre void, ou seja, não retorna nada.
* analogamente, qdo vc usa GET, tem q haver um retorno que é do mesmo tipo do atributo que será devolvido. Todo método Get tem que ter um return para retornar o valor.
* as IDE's mais populares (Netbeand e Eclipse) tem esse recurso embutido na própria IDE para facilitar a vida do programador.

Blz?
Abraços.

M

blz…vou ver se consigo fazer… agora mais duas duvidas…
sobre interface…estou fazendo as telas na mão pq tem q explicar o código na banca
e não estou conseguindo fazer os campos TextField ficarem do tamanho q eu desejo;por
ex o campo do nome ficar menor do q o do endereço e tbm fazer mascara do fone tbm…
Obrigado Desde ja… abraços

M

estou usando GridLayout

berg.pb

Como usar JTextField?

Criado 27 de setembro de 2008
Ultima resposta 29 de set. de 2008
Respostas 4
Participantes 2