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");
}
}
}