Advertisement
Guest User

ParaPablo

a guest
May 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. package JFrame;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. import java.util.Scanner;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JTextField;
  21. import javax.swing.JTextPane;
  22.  
  23. /**
  24. * @author Jose
  25. *
  26. */
  27. public class AgendaTelefono extends JFrame implements ActionListener {
  28. /**
  29. *
  30. */
  31.  
  32. private static final long serialVersionUID = 1L;
  33.  
  34. public Scanner teclado = new Scanner(System.in);
  35. public static HashMap<String, String> miLista = new HashMap<String, String>();
  36. public String nombre, telefono;
  37. public boolean encontrado = false;
  38.  
  39. private JButton A;
  40. private JButton B;
  41. private JButton L;
  42. private JButton V;
  43. private JButton M;
  44. private JButton BC;
  45. private JButton Guardar;
  46. private JButton Cargar;
  47. private JButton S;
  48. private JLabel Nombre;
  49. private JLabel Ordenes;
  50. private JLabel Lista;
  51. private JLabel Telefono;
  52. private JTextField jtf;
  53. private JTextField jtf2;
  54. private JTextPane jtf3;
  55. private JTextPane jtf4;
  56.  
  57. public static void guardarLista(HashMap<String, String> miLista) throws IOException {
  58. try {
  59. FileOutputStream fout = new FileOutputStream("Lista.ser");
  60. ObjectOutputStream oos = new ObjectOutputStream(fout);
  61. oos.writeObject(miLista);
  62. oos.close();
  63. fout.close();
  64. System.out.println("Se han guardado los datos correctamente.");
  65. } catch (Exception ex) {
  66. ex.printStackTrace();
  67. // System.out.println("Mensaje de la excepción: " );
  68. System.err.println("ERROR al guardar");
  69. }
  70. }
  71.  
  72. @SuppressWarnings("unchecked")
  73. public static void Cargar() throws FileNotFoundException, IOException, ClassNotFoundException {
  74. try {
  75. FileInputStream fileIn = new FileInputStream("Lista.ser");
  76. ObjectInputStream ino = new ObjectInputStream(fileIn);
  77. AgendaTelefono.miLista = (HashMap<String, String>) ino.readObject();
  78.  
  79. ino.close();
  80. fileIn.close();
  81. } catch (Exception ex) {
  82. System.err.println("Error al cargar los datos.");
  83. ex.printStackTrace();
  84. }
  85. }
  86.  
  87. public AgendaTelefono() {
  88. this.getContentPane().setBackground(Color.lightGray);
  89.  
  90. this.setTitle("Agenda Telefónica");
  91.  
  92. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93.  
  94. this.setBounds(200, 200, 1100, 600);
  95.  
  96. Container panel = this.getContentPane();
  97.  
  98. panel.setLayout(null);
  99. this.setBackground(Color.RED);
  100. A = new JButton("Añadir");
  101. B = new JButton("Borrar");
  102. L = new JButton("Listar");
  103. V = new JButton("Vaciar");
  104. M = new JButton("Modificar");
  105. BC = new JButton("Buscar");
  106. S = new JButton("Salir");
  107. Guardar = new JButton("Guardar");
  108. Guardar.addActionListener((java.awt.event.ActionEvent evt) -> {
  109. try {
  110. guardarLista(miLista);
  111. } catch (IOException ex) {
  112. System.err.print("Error");
  113. }
  114. });
  115. // System.out.println("Datos guardados.");
  116. Cargar = new JButton("Cargar");
  117. Cargar.addActionListener((java.awt.event.ActionEvent evt) -> {
  118. try {
  119. Cargar();
  120. System.out.println("Datos cargados." + "\n" + "Partida pausada.");
  121. } catch (IOException ex) {
  122. System.err.print("Error");
  123. } catch (ClassNotFoundException ex) {
  124. System.err.print("Error");
  125. }
  126. });
  127. Nombre = new JLabel("Nombre: ");
  128. Telefono = new JLabel("Teléfono: ");
  129. Ordenes = new JLabel("Ordenes");
  130. Lista = new JLabel("Lista");
  131. jtf = new JTextField("Introducir nombre");
  132. jtf2 = new JTextField("Introducir número");
  133. jtf3 = new JTextPane();
  134. jtf4 = new JTextPane();
  135.  
  136. Nombre.setBounds(50, 75, 100, 50);
  137. Telefono.setBounds(50, 225, 100, 50);
  138. Guardar.setBounds(200, 500, 100, 50);
  139. Cargar.setBounds(400, 500, 100, 50);
  140. jtf.setBounds(125, 75, 100, 60);
  141. jtf2.setBounds(125, 225, 100, 60);
  142. jtf3.setBounds(250, 50, 350, 300);
  143. jtf4.setBounds(650, 50, 350, 300);
  144. A.setBounds(50, 400, 100, 50);
  145. B.setBounds(200, 400, 100, 50);
  146. L.setBounds(350, 400, 100, 50);
  147. V.setBounds(500, 400, 100, 50);
  148. M.setBounds(650, 400, 100, 50);
  149. BC.setBounds(800, 400, 100, 50);
  150. S.setBounds(950, 400, 100, 50);
  151. Ordenes.setBounds(400, 5, 100, 50);
  152. Lista.setBounds(800, 5, 100, 50);
  153.  
  154. A.addActionListener(this);
  155. B.addActionListener(this);
  156. L.addActionListener(this);
  157. V.addActionListener(this);
  158. M.addActionListener(this);
  159. BC.addActionListener(this);
  160. S.addActionListener(this);
  161.  
  162. panel.add(Nombre);
  163. panel.add(Telefono);
  164. panel.add(A);
  165. panel.add(B);
  166.  
  167. panel.add(L);
  168. panel.add(V);
  169. panel.add(M);
  170. panel.add(BC);
  171. panel.add(S);
  172. panel.add(Ordenes);
  173. panel.add(Lista);
  174. panel.add(jtf);
  175. panel.add(jtf2);
  176. panel.add(jtf3);
  177. panel.add(jtf4);
  178. panel.add(Guardar);
  179. panel.add(Cargar);
  180.  
  181. 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"
  182. + "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");
  183. this.setVisible(true);
  184.  
  185. }
  186.  
  187. @Override
  188.  
  189. public void actionPerformed(ActionEvent e) {
  190. Object obj = e.getSource();
  191.  
  192. if (obj == A) {
  193. jtf.getText();
  194. jtf2.getText();
  195. miLista.put(jtf.getText(), jtf2.getText());
  196.  
  197. jtf4.setText("Nombre introducido: " + jtf.getText() + "\nTelefono introducido: " + jtf2.getText());
  198.  
  199. System.out.print(miLista);
  200. }
  201.  
  202. if (obj == B) {
  203.  
  204. jtf.getText();
  205. if (miLista.containsKey(jtf.getText())) {
  206. miLista.remove(jtf.getText());
  207. jtf4.setText("Nombre eliminado correctamente");
  208. } else {
  209. jtf4.setText("Nombre no econtrado");
  210. }
  211. }
  212.  
  213. if (obj == L) {
  214. String list = "";
  215.  
  216. Iterator<String> it2 = miLista.keySet().iterator();
  217. while (it2.hasNext()) {
  218.  
  219. Object clave = it2.next();
  220.  
  221. list += ("Nombre: " + clave + ", telefono: " + miLista.get(clave) + "\n");
  222. }
  223. jtf4.setText(list);
  224.  
  225. }
  226.  
  227. if (obj == V) {
  228.  
  229. System.out.print("\n");
  230. miLista.clear();
  231. jtf4.setText("Lista vaciada");
  232.  
  233. }
  234. if (obj == M) {
  235.  
  236. jtf.getText();
  237. if (miLista.containsKey(jtf.getText())) {
  238. miLista.remove(jtf.getText());
  239. jtf4.setText("Nombre listo para ser modificado");
  240. } else {
  241. jtf4.setText("Nombre no econtrado");
  242. }
  243. }
  244. if (obj == BC) {
  245.  
  246. jtf.getText();
  247. if (miLista.containsKey(jtf.getText())) {
  248. jtf4.setText("Nombre: " + jtf.getText() + ", telefono: " + miLista.get(jtf.getText()));
  249. } else {
  250. jtf4.setText("Nombre no econtrado");
  251. }
  252. }
  253. if (obj == S) {
  254.  
  255. System.out.println("LFin del programa");
  256.  
  257. }
  258.  
  259. }
  260. public static void main(String[]args){
  261. new AgendaTelefono();
  262. }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement