Boa noite, pessoal.
sou iniciante na área da programação, e por isso a dúvida deve ser bem óbvia, mas peço que me ajudem
Estou criando um sistema simples de agenda que o professor pediu, tenho as classes
Contatos, que tem os atributos, métodos de acesso e modificadores.
tenho a TelaAgenda, que criei a interface gráfica e que mandei passar as informações inseridas para os atributos da classe Contatos
e a classe Agenda, que contém um Array para mostrar os contatos inseridos. Porém, ao digitar as informações na tela agenda, elas não vão para os atributos da classe Contato, como eu esperava que fosse. Por que isso ocorre?
// CLASSE AONDE CONTÉM OS ATRIBUTOS COM OS MÉTODOS DE ACESSO E MODIFICADORES DOS CONTATOS.
public class Contatos {
private int id;
private String nome;
private String telefone;
private String email;
private String rua;
private String numero;
private String bairro;
private String cidade;
private String estado;
public Contatos() {
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
this.rua = rua;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "Nome: " + getNome() + " Telefone: " + getTelefone() + " Email: " + getEmail();
}
}
======================= TELA AGENDA =========
// CLASSE QUE MONTA A INTERFACE GRÁFICA E MANDA AS INFORMAÇÕES PARA OS ATRIBUTOS DA CLASSE CONTATOS.
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class TelaAgenda extends JFrame implements ActionListener {
Contatos contato = new Contatos();
private JLabel lblNome;
private JTextField txtNome;
private JLabel lblTelefone;
private JTextField txtTelefone;
private JLabel lblEmail;
private JTextField txtEmail;
private JLabel lblRua;
private JTextField txtRua;
private JLabel lblNumero;
private JTextField txtNumero;
private JLabel lblBairro;
private JTextField txtBairro;
private JLabel lblCidade;
private JTextField txtCidade;
private JLabel lblEstado;
private JTextField txtEstado;
private JButton btnAdicionar;
private JButton btnSair;
public TelaAgenda() {
super ("AGENDA");
lblNome = new JLabel (" Nome: ");
lblTelefone = new JLabel (" Telefone: ");
lblEmail = new JLabel (" Email: ");
lblRua = new JLabel (" Rua: ");
txtRua = new JTextField ("", 10);
lblNumero = new JLabel (" Número da residência :", 10);
txtNumero = new JTextField ("", 10);
lblBairro = new JLabel (" Bairro: ", 10);
txtBairro = new JTextField ("", 10);
lblCidade = new JLabel (" Cidade: ", 10);
txtCidade = new JTextField ("", 10);
lblEstado = new JLabel (" Estado : ", 10);
txtEstado = new JTextField ("", 10);
btnAdicionar = new JButton ("Adicionar");
btnSair = new JButton ("Sair");
txtNome = new JTextField ("", 10);
txtTelefone = new JTextField ("", 10);
txtEmail = new JTextField ("", 10);
Container tela = getContentPane();
tela.setLayout(new GridLayout(10,5));
tela.add(lblNome);
tela.add(txtNome);
tela.add(lblTelefone);
tela.add(txtTelefone);
tela.add(lblEmail);
tela.add(txtEmail);
tela.add(lblRua);
tela.add(txtRua);
tela.add(lblNumero);
tela.add(txtNumero);
tela.add(lblBairro);
tela.add(txtBairro);
tela.add(lblCidade);
tela.add(txtCidade);
tela.add(lblEstado);
tela.add(txtEstado);
tela.add (btnAdicionar);
tela.add(btnSair);
btnAdicionar.addActionListener(this);
btnSair.addActionListener(this);
setSize(580, 420);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== btnAdicionar) {
contato.setNome(txtNome.getText());
contato.setTelefone(txtTelefone.getText());
contato.setEmail(txtEmail.getText());
contato.setRua(txtRua.getText());
contato.setNumero(txtNumero.getText());
contato.setBairro(txtBairro.getText());
contato.setCidade(txtCidade.getText());
contato.setEstado(txtEstado.getText());
txtNome.setText("");
txtTelefone.setText("");
txtEmail.setText("");
txtRua.setText("");
txtNumero.setText("");
txtBairro.setText("");
txtCidade.setText("");
txtEstado.setText("");
} else if (e.getSource() == btnSair) {
TelaAgenda.this.dispose();
}
}
}
==================== AGENDA =================
import java.util.ArrayList;
//CLASSE AONDE TEM O ARRAYLIST E É FEITA A INCLUSÃO DE NOVOS CLIENTES INSTANCIANDO O OBJETO CONTATOS.
public class Agenda {
public static void main(String[] args) {
ArrayList <Contatos> array = new ArrayList();
Contatos c1 = new Contatos();
int ContID = 0 ;
array.add(c1);
System.out.println(c1.getNome());
for (int i=0; i<array.size(); i++) {
System.out.println("========================");
System.out.println(" ID: " + array.get(i).getId());
System.out.println(" Nome: " + array.get(i).getNome());
System.out.println("Telefone: " + array.get(i).getTelefone());
System.out.println(" Email: " + array.get(i).getEmail());
System.out.println(" Rua: " + array.get(i).getRua());
System.out.println(" Número: " + array.get(i).getRua());
}
}
}