Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JFrame;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JTextPane;
- /**
- * @author Jose
- *
- */
- public class AgendaTelefono extends JFrame implements ActionListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public Scanner teclado = new Scanner(System.in);
- public static HashMap<String, String> miLista = new HashMap<String, String>();
- public String nombre, telefono;
- public boolean encontrado = false;
- private JButton A;
- private JButton B;
- private JButton L;
- private JButton V;
- private JButton M;
- private JButton BC;
- private JButton Guardar;
- private JButton Cargar;
- private JButton S;
- private JLabel Nombre;
- private JLabel Ordenes;
- private JLabel Lista;
- private JLabel Telefono;
- private JTextField jtf;
- private JTextField jtf2;
- private JTextPane jtf3;
- private JTextPane jtf4;
- public static void guardarLista(HashMap<String, String> miLista) throws IOException {
- try {
- FileOutputStream fout = new FileOutputStream("Lista.ser");
- ObjectOutputStream oos = new ObjectOutputStream(fout);
- oos.writeObject(miLista);
- oos.close();
- fout.close();
- System.out.println("Se han guardado los datos correctamente.");
- } catch (Exception ex) {
- ex.printStackTrace();
- // System.out.println("Mensaje de la excepción: " );
- System.err.println("ERROR al guardar");
- }
- }
- @SuppressWarnings("unchecked")
- public static void Cargar() throws FileNotFoundException, IOException, ClassNotFoundException {
- try {
- FileInputStream fileIn = new FileInputStream("Lista.ser");
- ObjectInputStream ino = new ObjectInputStream(fileIn);
- AgendaTelefono.miLista = (HashMap<String, String>) ino.readObject();
- ino.close();
- fileIn.close();
- } catch (Exception ex) {
- System.err.println("Error al cargar los datos.");
- ex.printStackTrace();
- }
- }
- public AgendaTelefono() {
- this.getContentPane().setBackground(Color.lightGray);
- this.setTitle("Agenda Telefónica");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setBounds(200, 200, 1100, 600);
- Container panel = this.getContentPane();
- panel.setLayout(null);
- this.setBackground(Color.RED);
- A = new JButton("Añadir");
- B = new JButton("Borrar");
- L = new JButton("Listar");
- V = new JButton("Vaciar");
- M = new JButton("Modificar");
- BC = new JButton("Buscar");
- S = new JButton("Salir");
- Guardar = new JButton("Guardar");
- Guardar.addActionListener((java.awt.event.ActionEvent evt) -> {
- try {
- guardarLista(miLista);
- } catch (IOException ex) {
- System.err.print("Error");
- }
- });
- // System.out.println("Datos guardados.");
- Cargar = new JButton("Cargar");
- Cargar.addActionListener((java.awt.event.ActionEvent evt) -> {
- try {
- Cargar();
- System.out.println("Datos cargados." + "\n" + "Partida pausada.");
- } catch (IOException ex) {
- System.err.print("Error");
- } catch (ClassNotFoundException ex) {
- System.err.print("Error");
- }
- });
- Nombre = new JLabel("Nombre: ");
- Telefono = new JLabel("Teléfono: ");
- Ordenes = new JLabel("Ordenes");
- Lista = new JLabel("Lista");
- jtf = new JTextField("Introducir nombre");
- jtf2 = new JTextField("Introducir número");
- jtf3 = new JTextPane();
- jtf4 = new JTextPane();
- Nombre.setBounds(50, 75, 100, 50);
- Telefono.setBounds(50, 225, 100, 50);
- Guardar.setBounds(200, 500, 100, 50);
- Cargar.setBounds(400, 500, 100, 50);
- jtf.setBounds(125, 75, 100, 60);
- jtf2.setBounds(125, 225, 100, 60);
- jtf3.setBounds(250, 50, 350, 300);
- jtf4.setBounds(650, 50, 350, 300);
- A.setBounds(50, 400, 100, 50);
- B.setBounds(200, 400, 100, 50);
- L.setBounds(350, 400, 100, 50);
- V.setBounds(500, 400, 100, 50);
- M.setBounds(650, 400, 100, 50);
- BC.setBounds(800, 400, 100, 50);
- S.setBounds(950, 400, 100, 50);
- Ordenes.setBounds(400, 5, 100, 50);
- Lista.setBounds(800, 5, 100, 50);
- A.addActionListener(this);
- B.addActionListener(this);
- L.addActionListener(this);
- V.addActionListener(this);
- M.addActionListener(this);
- BC.addActionListener(this);
- S.addActionListener(this);
- panel.add(Nombre);
- panel.add(Telefono);
- panel.add(A);
- panel.add(B);
- panel.add(L);
- panel.add(V);
- panel.add(M);
- panel.add(BC);
- panel.add(S);
- panel.add(Ordenes);
- panel.add(Lista);
- panel.add(jtf);
- panel.add(jtf2);
- panel.add(jtf3);
- panel.add(jtf4);
- panel.add(Guardar);
- panel.add(Cargar);
- jtf3.setText("IMPORTANTE!! Crear una carpeta en el escritorio y poner el programa en esa carpeta\n\nAñadir: Introduce el nombre y el telefono en su casilla correspondiente y dale al boton.\n" + "\n"
- + "Borrar: Introduzca el nombre para borrar y dale al boton\n\nModificar: Introduce el nombre en la casilla Nombre y dele al boton, despues realice los cambios y dale a añadirnombre\n\nBuscar: Introduce el nombre y dale a buscar");
- this.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Object obj = e.getSource();
- if (obj == A) {
- jtf.getText();
- jtf2.getText();
- miLista.put(jtf.getText(), jtf2.getText());
- jtf4.setText("Nombre introducido: " + jtf.getText() + "\nTelefono introducido: " + jtf2.getText());
- System.out.print(miLista);
- }
- if (obj == B) {
- jtf.getText();
- if (miLista.containsKey(jtf.getText())) {
- miLista.remove(jtf.getText());
- jtf4.setText("Nombre eliminado correctamente");
- } else {
- jtf4.setText("Nombre no econtrado");
- }
- }
- if (obj == L) {
- String list = "";
- Iterator<String> it2 = miLista.keySet().iterator();
- while (it2.hasNext()) {
- Object clave = it2.next();
- list += ("Nombre: " + clave + ", telefono: " + miLista.get(clave) + "\n");
- }
- jtf4.setText(list);
- }
- if (obj == V) {
- System.out.print("\n");
- miLista.clear();
- jtf4.setText("Lista vaciada");
- }
- if (obj == M) {
- jtf.getText();
- if (miLista.containsKey(jtf.getText())) {
- miLista.remove(jtf.getText());
- jtf4.setText("Nombre listo para ser modificado");
- } else {
- jtf4.setText("Nombre no econtrado");
- }
- }
- if (obj == BC) {
- jtf.getText();
- if (miLista.containsKey(jtf.getText())) {
- jtf4.setText("Nombre: " + jtf.getText() + ", telefono: " + miLista.get(jtf.getText()));
- } else {
- jtf4.setText("Nombre no econtrado");
- }
- }
- if (obj == S) {
- System.out.println("LFin del programa");
- }
- }
- public static void main(String[]args){
- new AgendaTelefono();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement