Cadastro de Alunos

6 respostas
G

Boa tarde pessoal.
Estudo Java já há duas semanas. Tenho um Exercicio de escrever um código para cadastrar alunos. A minha dificuldade está em armazenar os dados introduzidos no JComboBox. Tenho 2 botões, 1 para CADASTRAR e o outro para VISUALIZAR os alunos cadastrados.
Peço ajuda a quem puder…

package Exercicios;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Teste3_Cadastro extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 1L;

JTextField nome, email, telefone, curso;
JLabel nomeLbl, emailLbl, telefoneLbl, cursoLbl, titulo;
JPanel panel, panel0, panel1, panel2, panel3, panel4, panel5;
JButton btn, btn1;
JComboBox cursosCB;

Teste3_Cadastro(){
    
    super("Above - Formação Profissional");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
    String [] cursos = {"---Escolha o Curso que deseja frequentar---","Técnico de Informática e S.E", "Técnico Administrativo", "Técnico de Electricidade e Refrigeração", 
    "Técnico de Comunicação e Multimédia", "Câmeras, Alarmes, Cercas e Portões", "Higiene e Segurança no Trabalho", "AutoCAD"};
    cursosCB = new JComboBox(cursos);
    cursosCB.setPreferredSize(new Dimension(450,50));
    
    
    panel = new JPanel(new GridLayout(6, 1));
    Font font = new Font("Comic Sans Ms", Font.BOLD, 20);
    Font font1 = new Font("Comic Sans Ms", Font.BOLD, 18);
    Font font2 = new Font("Gabriola", Font.BOLD, 15);
    
    panel0 = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    panel4 = new JPanel();
    
    titulo = new JLabel("PREENCHA OS CAMPOS ABAIXO");
    nomeLbl = new JLabel("Nome");
    emailLbl = new JLabel("Email");
    telefoneLbl = new JLabel("Telefone");
    cursoLbl = new JLabel("Curso");
    
    Color c = new Color(4, 187, 247);
    titulo.setForeground(c);
    titulo.setFont(font);
    
    nome = new JTextField();
    nome.setPreferredSize(new Dimension(450,50));
    email = new JTextField();
    email.setPreferredSize(new Dimension(450,50));
    telefone = new JTextField();
    telefone.setPreferredSize(new Dimension(450,50));
    curso = new JTextField();
    
    btn = new JButton("Cadastrar");
    btn.setForeground(Color.white);
    btn.setBackground(c);
    btn.setFont(font1);
    btn.setPreferredSize(new Dimension(140,40));
    
    btn1 = new JButton("Visualizar");
    btn1.setForeground(Color.white);
    btn1.setBackground(c);
    btn1.setFont(font1);
    btn1.setPreferredSize(new Dimension(140,40));
    
    btn.addActionListener(this);
    btn1.addActionListener(this);
    
    panel0.add(titulo);
    
    panel1.add(nomeLbl);
    panel1.add(nome);
    nomeLbl.setForeground(c);
    nome.setFont(font2);
    nomeLbl.setFont(font1);
    
    panel2.add(emailLbl);
    panel2.add(email);
    emailLbl.setForeground(c);
    email.setFont(font2);
    emailLbl.setFont(font1);
    
    panel3.add(telefoneLbl);
    panel3.add(telefone);
    telefoneLbl.setForeground(c);
    telefone.setFont(font2);
    telefoneLbl.setFont(font1);
    
    panel4.add(cursoLbl);
    panel4.add(cursosCB);
    cursoLbl.setForeground(c);
    cursoLbl.setFont(font1);
    cursosCB.setBackground(Color.white);
    cursosCB.setFont(font1);
    
    panel5 = new JPanel(new FlowLayout());
    panel5.add(btn, JButton.CENTER);
    panel5.add(btn1);
    
    panel0.setBackground(Color.white);
    panel1.setBackground(Color.white);
    panel2.setBackground(Color.white);
    panel3.setBackground(Color.white);
    panel4.setBackground(Color.white);
    panel5.setBackground(Color.white);
    
    
    
    panel.add(panel0);
    panel.add(panel1);
    panel.add(panel2);
    panel.add(panel3);
    panel.add(panel4);
    panel.add(panel5);

    add(panel);

    setResizable(false);
    setSize(700,700);
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args) {
    new Teste3_Cadastro();

}


@Override
public void actionPerformed(ActionEvent ae) {
    if(ae.getSource().equals(btn)){

        int posicao = cursosCB.getSelectedIndex();
        
        JOptionPane.showMessageDialog(null, "Cadastrado com sucesso");    
    }
    else if(ae.getSource().equals(btn1)){
        
        JOptionPane.showMessageDialog(null, "Sem Sucesso");
        
    }
            
}
    
}

6 Respostas

D

Olá

Vc pode usar a classe LinkedList

private LinkedList<Object[]> listaDeAlunos = new LinkedList<Object[]>();

// cadastrar
Object[] aluno = new Object[2];
aluno[0] = nome.getText();
aluno[1] = cursos.getSelectedItem();
listaDeAlunos.add(aluno);

// visualizar
for (Object[] aluno : listaDeAlunos) {
    System.out.print("Nome: ");
    System.out.println(aluno[0]);
    System.out.print("Curso: ");
    System.out.println(aluno[1]);
    System.out.println("---------------------");
}

Se já souber usar classes, então use

class Aluno {
    String nome;
    String curso;
}
G

Muito obrigado diego12

G

Usando o JOptionPane, como ficaria?

A

Olá Diego,boa tarde!

Tb só tenho duas semanas em java e meu professor solicitou que fizéssemos uma agenda no java Fx em aplicação swing. Aí ficou assim minha interface, mas tenho que fazer esses botoes funcionar corretamente e acrescentar o botão incluir tb. Será que vc poderia me dar uma ajudinha? Eu não sei nada a não ser criar interfaces.

Enviando…

J

Não vi codigo relacionado com cadastro no teu codigo. Duas semanas so? Talvez vc esteja muito focado em fazer a aparencia das janelas rsrs e esquecendo o principal.

package corrida_lebres;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

public class Teste3_Cadastro extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

JTextField nome, email, telefone, curso;
JLabel nomeLbl, emailLbl, telefoneLbl, cursoLbl, titulo;
JPanel panel, panel0, panel1, panel2, panel3, panel4, panel5;
JButton btn, btn1;
JComboBox cursosCB;

Teste3_Cadastro(){

    super("Above - Formação Profissional");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String [] cursos = {"---Escolha o Curso que deseja frequentar---","Técnico de Informática e S.E", "Técnico Administrativo", "Técnico de Electricidade e Refrigeração", 
    "Técnico de Comunicação e Multimédia", "Câmeras, Alarmes, Cercas e Portões", "Higiene e Segurança no Trabalho", "AutoCAD"};
    cursosCB = new JComboBox(cursos);
    cursosCB.setPreferredSize(new Dimension(450,50));


    panel = new JPanel(new GridLayout(6, 1));
    Font font = new Font("Comic Sans Ms", Font.BOLD, 20);
    Font font1 = new Font("Comic Sans Ms", Font.BOLD, 18);
    Font font2 = new Font("Gabriola", Font.BOLD, 15);

    panel0 = new JPanel();
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    panel4 = new JPanel();

    titulo = new JLabel("PREENCHA OS CAMPOS ABAIXO");
    nomeLbl = new JLabel("Nome");
    emailLbl = new JLabel("Email");
    telefoneLbl = new JLabel("Telefone");
    cursoLbl = new JLabel("Curso");

    Color c = new Color(4, 187, 247);
    titulo.setForeground(c);
    titulo.setFont(font);

    nome = new JTextField();
    nome.setPreferredSize(new Dimension(450,50));
    email = new JTextField();
    email.setPreferredSize(new Dimension(450,50));
    telefone = new JTextField();
    telefone.setPreferredSize(new Dimension(450,50));
    curso = new JTextField();

    btn = new JButton("Cadastrar");
    btn.setForeground(Color.white);
    btn.setBackground(c);
    btn.setFont(font1);
    btn.setPreferredSize(new Dimension(140,40));

    btn1 = new JButton("Visualizar");
    btn1.setForeground(Color.white);
    btn1.setBackground(c);
    btn1.setFont(font1);
    btn1.setPreferredSize(new Dimension(140,40));

    btn.addActionListener(this);
    btn1.addActionListener(this);

    panel0.add(titulo);

    panel1.add(nomeLbl);
    panel1.add(nome);
    nomeLbl.setForeground(c);
    nome.setFont(font2);
    nomeLbl.setFont(font1);

    panel2.add(emailLbl);
    panel2.add(email);
    emailLbl.setForeground(c);
    email.setFont(font2);
    emailLbl.setFont(font1);

    panel3.add(telefoneLbl);
    panel3.add(telefone);
    telefoneLbl.setForeground(c);
    telefone.setFont(font2);
    telefoneLbl.setFont(font1);

    panel4.add(cursoLbl);
    panel4.add(cursosCB);
    cursoLbl.setForeground(c);
    cursoLbl.setFont(font1);
    cursosCB.setBackground(Color.white);
    cursosCB.setFont(font1);

    panel5 = new JPanel(new FlowLayout());
    panel5.add(btn, JButton.CENTER);
    panel5.add(btn1);

    panel0.setBackground(Color.white);
    panel1.setBackground(Color.white);
    panel2.setBackground(Color.white);
    panel3.setBackground(Color.white);
    panel4.setBackground(Color.white);
    panel5.setBackground(Color.white);



    panel.add(panel0);
    panel.add(panel1);
    panel.add(panel2);
    panel.add(panel3);
    panel.add(panel4);
    panel.add(panel5);

    add(panel);

    setResizable(false);
    setSize(700,700);
    setLocationRelativeTo(null);
}

public static void main(String[] args) {
   Teste3_Cadastro abre =  new Teste3_Cadastro();
   abre.setVisible(true);

}



 public Teste3_Cadastro getCadastro() {
     Teste3_Cadastro cadastra= new Teste3_Cadastro();
     cadastra.setNomes(nome.getText());
     cadastra.setTelefones(Integer.parseInt(telefone.getText()));
     cadastra.setEmails(email.getText());
     cadastra.setCursos(cursosCB.getSelectedItem());
    return cadastra;
 }
 
 public ArrayList<Teste3_Cadastro> getLista() {
     Teste3_Cadastro cadastro = getCadastro();
     ArrayList<Teste3_Cadastro> lista = new ArrayList<>();
     lista.add(cadastro);
     return lista;
 }
 
@Override
public void actionPerformed(ActionEvent ae) {
    if(ae.getSource().equals(btn)){
         getCadastro();
          JOptionPane.showMessageDialog(null, "Cadastrado com sucesso");
    }
    else if(ae.getSource().equals(btn1)){
        System.out.println(getLista());
        JOptionPane.showMessageDialog(null, getLista());   
    }
            
}
  
private String nomes;
private int telefones;
private Object cursos;
private String emails;


    public String getNomes() {
        return nomes;
    }

    public void setNomes(String nomes) {
        this.nomes = nomes;
    }

    public int getTelefones() {
        return telefones;
    }

    public void setTelefones(int telefones) {
        this.telefones = telefones;
    }

    public Object getCursos() {
        return cursos;
    }

    public void setCursos(Object cursos) {
        this.cursos = cursos;
    }

    public String getEmails() {
        return emails;
    }

    public void setEmails(String emails) {
        this.emails = emails;
    }

 

    @Override
    public String toString() {
        return "Cadastro: \n" + 
                "Curso: "+ cursos+ "\n "+
                "Nome: " + nomes +  "\n "+
                "Telefone: " + telefones +  "\n "+
                "Email: " + emails+"\n\n";
    }


    

}
G

Obrigado pela observação. Estou a melhorar o código. Realmente fiquei focado na Interface…
Abraço.

Criado 26 de junho de 2016
Ultima resposta 27 de jun. de 2016
Respostas 6
Participantes 4