Tratamento de eventos

0 respostas
J

Boa noite,

estou "tentando" fazer um projeto em java, que basicamente é uma árvore com interface gráfica e tem muito que aprimorar ainda.

empaquei quando não consigo na linha 63 da classe telaprincipal mostrar a árvore cujo dados foram inseridos na telaInserçaoAlunos

segue código abaixo.

Desde já agradeço

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;

import com.jgoodies.forms.factories.DefaultComponentFactory;


public class TelaPrincipal {

	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaPrincipal window = new TelaPrincipal();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public TelaPrincipal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnInserirAluno = new JButton("Inserir Aluno na \u00C1rvore");
		btnInserirAluno.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TelaInserçaoAluno t1 = new TelaInserçaoAluno();
				t1.setVisible(true);
			}
		});
		btnInserirAluno.setBounds(125, 81, 190, 23);
		frame.getContentPane().add(btnInserirAluno);
		
		
		JButton btnRemoverAluno = new JButton("Remover Aluno da \u00C1rvore");
		btnRemoverAluno.setBounds(125, 115, 190, 23);
		frame.getContentPane().add(btnRemoverAluno);
		
		JButton btnMostrarArvore = new JButton("Mostrar \u00C1rvore");
		btnMostrarArvore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		btnMostrarArvore.setBounds(125, 183, 190, 23);
		frame.getContentPane().add(btnMostrarArvore);
		
		JButton btnBuscarAluno = new JButton("Buscar Aluno");
		btnBuscarAluno.setBounds(125, 149, 190, 23);
		frame.getContentPane().add(btnBuscarAluno);
		
		JButton btnSair = new JButton("Sair");
		btnSair.setBounds(125, 217, 190, 23);
		frame.getContentPane().add(btnSair);
		
		JLabel lblQualOperaoDeseja = new JLabel("Qual opera\u00E7\u00E3o deseja realizar na sua \u00C1rvore de Alunos?");
		lblQualOperaoDeseja.setBounds(67, 45, 333, 25);
		frame.getContentPane().add(lblQualOperaoDeseja);
		
		JLabel lblrvoreDeAlunos = DefaultComponentFactory.getInstance().createTitle("\u00C1rvore de Alunos");
		lblrvoreDeAlunos.setBounds(154, 11, 124, 14);
		frame.getContentPane().add(lblrvoreDeAlunos);
	}
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;


public class TelaInserçaoAluno extends JFrame {

	private JPanel contentPane;
	private JTextField nomeText;
	private JTextField matriculaText;
	private JTextField cursoText;
	private JTextField cpfText;
	private int qtdAlunos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaInserçaoAluno frame = new TelaInserçaoAluno();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaInserçaoAluno() {
		

		

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("Nome:");
		label.setBounds(29, 43, 64, 14);
		contentPane.add(label);
		
		nomeText = new JTextField();
		nomeText.setColumns(10);
		nomeText.setBounds(110, 40, 236, 20);
		contentPane.add(nomeText);
		
		matriculaText = new JTextField(10);
		matriculaText.setText("0");
		matriculaText.setBounds(110, 139, 130, 20);
		contentPane.add(matriculaText);
		matriculaText.setText(String.valueOf(0)); 
				
		cursoText = new JTextField();
		cursoText.setColumns(10);
		cursoText.setBounds(110, 175, 219, 20);
		contentPane.add(cursoText);
		
		
		cpfText = new JTextField();
		cpfText.setColumns(10);
		cpfText.setBounds(110, 71, 130, 20);
		contentPane.add(cpfText);
		
		
		JLabel label_1 = new JLabel("Cpf:");
		label_1.setBounds(29, 72, 64, 14);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("Idade:");
		label_2.setBounds(29, 107, 64, 14);
		contentPane.add(label_2);
		
		final JComboBox idadeCombo = new JComboBox();
		idadeCombo.setBounds(110, 104, 42, 20);
		idadeCombo.setModel(new DefaultComboBoxModel(new String[] {"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70"}));
		contentPane.add(idadeCombo);
		
		
		JLabel label_3 = new JLabel("Matricula:");
		label_3.setBounds(29, 142, 64, 14);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("Curso:");
		label_4.setBounds(29, 178, 64, 14);
		contentPane.add(label_4);
		
		JButton previzualizar = new JButton("Pr\u00E9 vizualiza\u00E7\u00E3o");
		previzualizar.setBounds(19, 228, 133, 23);
		previzualizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				JOptionPane.showMessageDialog(null,"Pré-vizualização:  "+"\n"+"\n" + "Nome: " + nomeText.getText() +"\n" +
		        		"CPF: " + cpfText.getText() +"\n" + "Idade: " + Integer.parseInt((String) idadeCombo.getSelectedItem()) +"\n" +
		        		"Matricula: " +Integer.parseInt(matriculaText.getText()) +"\n" + "Curso: " + cursoText.getText() +"\n");
				
			}
		});
		contentPane.add(previzualizar);
		
		JButton salvar = new JButton("Salvar");
		salvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Aluno a1 = new Aluno();
				No no = new No(a1);
				Arvore arvoreBinaria = new Arvore();

				a1.setNome(nomeText.getText());
				a1.setCpf(cpfText.getText());
				a1.setIdade(Integer.parseInt((String)idadeCombo.getSelectedItem()));
				a1.setMatricula(Integer.parseInt(matriculaText.getText()));
				a1.setCurso(cursoText.getText());
				arvoreBinaria.inserirAluno(no);
				qtdAlunos++;
			}
		});
		salvar.setBounds(172, 228, 116, 23);
		contentPane.add(salvar);
		
		JButton cancelar = new JButton("Cancelar");
		cancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TelaInserçaoAluno.this.dispose();
			}
		});
		cancelar.setBounds(308, 228, 116, 23);
		contentPane.add(cancelar);
		
		
		
	}

}
import javax.swing.JOptionPane;


public class Arvore {
	
	private No raiz;
    private Arvore direita;
    private Arvore esquerda;
    
    
	   public void inserirAluno(No no) {
	        if (this.raiz == null) {
	            this.raiz = no;
	        } else {
	            if (no.getAluno().getMatricula() > this.raiz.getAluno().getMatricula()) {
	                if (this.direita == null) {
	                    this.direita = new Arvore();
	                }
	                this.direita.inserirAluno(no);
	            } else if (no.getAluno().getMatricula() < this.raiz.getAluno().getMatricula()) {
	                if (this.esquerda == null) {
	                    this.esquerda = new Arvore();
	                }
	                this.esquerda.inserirAluno(no);
	            }
	        }
	    }
	 
	    public void percorrerArvore() {
	        if (this.raiz == null) {
	           return;
	        }
	 
	        if (this.esquerda != null) {
	            this.esquerda.percorrerArvore();
	        }
	 
	        
	        JOptionPane.showMessageDialog(null, "Nome: " + this.raiz.getAluno().getNome() +"\n" +
	        		"CPF: " + this.raiz.getAluno().getCpf() +"\n" + "Idade: " + this.raiz.getAluno().getIdade() +"\n" +
	        		"Matricula: " + this.raiz.getAluno().getMatricula() +"\n" + "Curso: " + this.raiz.getAluno().getCurso() +"\n");
	        
	        if (this.direita != null) {
	            this.direita.percorrerArvore();
	        }
	    }
	 
	    
	    public void percorrerArvoreGrafico() {
	        if (this.raiz == null) {
	           return;
	        }
	 
	        if (this.esquerda != null) {
	            this.esquerda.percorrerArvore();
	        }
	 
	        
	        JOptionPane.showMessageDialog(null, "Matricula: " + this.raiz.getAluno().getMatricula() +"\n");
	        
	        if (this.direita != null) {
	            this.direita.percorrerArvore();
	        }
	    }
	   
	    public Aluno busca(int matricula) {
	        if (this.raiz == null) {
	            return null;
	        } else {
	            if (matricula == this.raiz.getAluno().getMatricula()) {
	                return this.raiz.getAluno();
	            } else {
	                if (matricula > this.raiz.getAluno().getMatricula()) {
	                    if (this.direita == null) {
	                        return null;
	                    }
	                    return this.direita.busca(matricula);
	                } else {
	                    if (this.esquerda == null) {
	                        return null;
	                    }
	                    return this.esquerda.busca(matricula);
	                }
	            }
	        }
	        
	    }
	    
	    public No getRaiz() {
			return raiz;
		}

		public void setRaiz(No raiz) {
			this.raiz = raiz;
		}

		public Arvore getDireita() {
			return direita;
		}

		public void setDireita(Arvore direita) {
			this.direita = direita;
		}

		public Arvore getEsquerda() {
			return esquerda;
		}

		public void setEsquerda(Arvore esquerda) {
			this.esquerda = esquerda;
		}

	    
}
Criado 16 de novembro de 2014
Respostas 0
Participantes 1