View.java
Código PHP:
package poofinal;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.String.*;
public class View extends JFrame {
Eventos evento = new Eventos();
View p = new View();
JPanel arriba = new JPanel ();
JPanel centro = new JPanel();
JPanel abajo = new JPanel();
JButton ok = new JButton ("OK");
JButton reset = new JButton ("Reset");
JButton cerrar = new JButton ("Cerrar");
JLabel nombre = new JLabel ("nombre");
JTextField nombreT = new JTextField(10);
JLabel matricula = new JLabel ("matricula");
JTextField matriculaT = new JTextField(10);
JLabel email = new JLabel ("email");
JTextField emailT = new JTextField(10);
JLabel mat1 = new JLabel ("mat1");
JTextField mat1T = new JTextField(5);
JLabel mat2 = new JLabel ("mat2");
JTextField mat2T = new JTextField(5);
JLabel mat3 = new JLabel ("mat3");
JTextField mat3T = new JTextField(5);
JLabel mat4 = new JLabel ("mat4");
JTextField mat4T = new JTextField(5);
JLabel mat5 = new JLabel ("mat5");
JTextField mat5T = new JTextField(5);
JLabel promedio = new JLabel ("promedio");
JTextField promedioT = new JTextField (5);
JMenu archivo = new JMenu ("Archivo");
JMenuBar barrita = new JMenuBar();
JMenuItem nuevo = new JMenuItem ("Nuevo");
JMenuItem borrar = new JMenuItem ("Borrar");
JMenuItem buscar = new JMenuItem ("Buscar");
public void incluyeElementos()
{
archivo.add(nuevo);
archivo.add(buscar);
archivo.add(borrar);
barrita.add(archivo);
abajo.add (ok);
abajo.add (reset);
abajo.add (cerrar);
arriba.add (nombre);
arriba.add(nombreT);
arriba.add (matricula);
arriba.add(matriculaT);
arriba.add (email);
arriba.add(emailT);
centro.add (mat1);
centro.add(mat1T);
centro.add (mat2);
centro.add(mat2T);
centro.add (mat3);
centro.add(mat3T);
centro.add (mat4);
centro.add(mat4T);
centro.add (mat5);
centro.add(mat5T);
centro.add(promedio);
centro.add(promedioT);
}
public void ocultarElementos()
{
nombreT.setEnabled(false);
matriculaT.setEnabled(false);
emailT.setEnabled(false);
mat1T.setEnabled(false);
mat2T.setEnabled(false);
mat3T.setEnabled(false);
mat4T.setEnabled(false);
mat5T.setEnabled(false);
promedioT.setEnabled(false);
}
public void adiereActionListener()
{
ok.addActionListener(evento);
cerrar.addActionListener(evento);
borrar.addActionListener(evento);
nuevo.addActionListener(evento);
buscar.addActionListener(evento);
reset.addActionListener(evento);
}
public View(){
p.incluyeElementos();
p.ocultarElementos();
p.adiereActionListener();
this.setLayout(new BorderLayout());
this.setJMenuBar(barrita);
this.add(ok);
this.add(reset);
this.add(cerrar);
this.add(nombre);
this.add(arriba, BorderLayout.NORTH);
this.add(centro, BorderLayout.CENTER);
this.add(abajo, BorderLayout.SOUTH);
this.setTitle("Proyecto Final POO");
this.setSize(700,250);
this.setVisible(true);
this.setDefaultCloseOperation(View.EXIT_ON_CLOSE);
}
public static void main(String[] args)throws IOException {
View program = new View ();
}
}
y
Eventos.java
Código PHP:
package poofinal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;
import java.lang.String.*;
public class Eventos implements ActionListener {
View v = new View();
public void limpiar()
{
v.matriculaT.setText("");
v.nombreT.setText("");
v.emailT.setText("");
v.mat1T.setText("");
v.mat2T.setText("");
v.mat3T.setText("");
v.mat4T.setText("");
v.mat5T.setText("");
v.promedioT.setText("");
}
public void elementosVisibles()
{
v.nombreT.setEnabled(true);
v.matriculaT.setEnabled(true);
v.emailT.setEnabled(true);
v.mat1T.setEnabled(true);
v.mat2T.setEnabled(true);
v.mat3T.setEnabled(true);
v.mat4T.setEnabled(true);
v.mat5T.setEnabled(true);
v.promedioT.setEnabled(true);
}
public void actionPerformed (ActionEvent E){
if(E.getSource()== v.nuevo)
{
limpiar();
elementosVisibles();
}
PrintWriter escribir;
if(E.getSource() == v.ok)
{
BufferedWriter arch = null;
try {
arch = new BufferedWriter(new FileWriter("C:\\alumnos2.txt", true));
escribir = new PrintWriter(arch);
float m1 = Float.parseFloat(v.mat1T.getText());
float m2 = Float.parseFloat(v.mat2T.getText());
float m3 = Float.parseFloat(v.mat3T.getText());
float m4 = Float.parseFloat(v.mat4T.getText());
float m5 = Float.parseFloat(v.mat5T.getText());
float promedioT = (m1+m2+m3+m4+m5)/5;
escribir.println( v.matriculaT.getText() + "," + v.nombreT.getText()+ "," + v.emailT.getText() + "," + v.mat1T.getText() + "," + v.mat2T.getText() + "," + v.mat3T.getText() + "," + v.mat4T.getText() + "," + v.mat5T.getText()+ "," + promedioT );
escribir.flush();
JOptionPane.showMessageDialog(null, "Se ha creado el usuario con exito! ;)");
limpiar();
} catch (IOException ex) {
Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (E.getSource() == v.cerrar){
System.exit(0);
}
if(E.getSource() == v.borrar)
{
try{
String matBuscar = JOptionPane.showInputDialog("Ingrese la matricula", "ej. 1493584...");
BufferedReader arch = new BufferedReader(new FileReader("C:\\alumnos2.txt"));
String a;
while((a = arch.readLine()) != null)
{
StringTokenizer matEncontrada = new StringTokenizer(a, ",");
if(!a.startsWith(matBuscar))
{
JOptionPane.showMessageDialog(null, "No se ha encontrado el usuario buscado");
}else{
while(matEncontrada.hasMoreTokens())
{
v.matriculaT.setText(matEncontrada.nextToken());
v.nombreT.setText(matEncontrada.nextToken());
v.emailT.setText(matEncontrada.nextToken());
v.mat1T.setText(matEncontrada.nextToken());
v.mat2T.setText(matEncontrada.nextToken());
v.mat3T.setText(matEncontrada.nextToken());
v.mat4T.setText(matEncontrada.nextToken());
v.mat5T.setText(matEncontrada.nextToken());
v.promedioT.setText(matEncontrada.nextToken());
}
}
}
}catch(IOException ex)
{
}
}
if (E.getSource()== v.reset){
limpiar();
}
if(E.getSource() == v.buscar)
{
try{
String matBuscar = JOptionPane.showInputDialog("Ingrese la matricula", "ej. 1493584...");
BufferedReader arch = new BufferedReader(new FileReader("C:\\alumnos2.txt"));
String a;
while((a = arch.readLine()) != null)
{
StringTokenizer matEncontrada = new StringTokenizer(a, ",");
if(!a.startsWith(matBuscar))
{
JOptionPane.showMessageDialog(null, "No se ha encontrado el usuario buscado");
}else{
while(matEncontrada.hasMoreTokens())
{
v.matriculaT.setText(matEncontrada.nextToken());
v.nombreT.setText(matEncontrada.nextToken());
v.emailT.setText(matEncontrada.nextToken());
v.mat1T.setText(matEncontrada.nextToken());
v.mat2T.setText(matEncontrada.nextToken());
v.mat3T.setText(matEncontrada.nextToken());
v.mat4T.setText(matEncontrada.nextToken());
v.mat5T.setText(matEncontrada.nextToken());
v.promedioT.setText(matEncontrada.nextToken());
}
}
}
}catch(IOException ex)
{
}
}
}
}