elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Uso del KeyListener
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Uso del KeyListener  (Leído 1,672 veces)
m@o_614


Desconectado Desconectado

Mensajes: 389


Ver Perfil
Uso del KeyListener
« en: 27 Agosto 2015, 22:50 pm »

Saludos, tengo el siguiente código el cuál me imprime el ID y el tiempo de duración de X número de procesos. El problema es que tengo que agregarle un evento de teclado, una vez que está corriendo el programa, si yo oprimo la tecla E me tiene que salir un mensaje de error y continuar con la ejecucion de procesos, y si oprimo la tecla S la ejecucion para por completo. Busqué información de keyListener y trate de agregarle el evento de teclado como pude, pero no funciona correctamente y no sé que estoy haciendo mal

Código
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.Random;
  4.  
  5. import javax.swing.*;
  6.  
  7. class Hilo extends Thread{
  8. private boolean puedeImprimir;
  9. private int ID,numeroCanicas,Tiempo;
  10. private JTextArea areaTexto;
  11. private JTextField campoTexto;
  12.  
  13. public Hilo(JTextArea areaTextox,int numeroCanicasx)
  14. {
  15. areaTexto = areaTextox;
  16. puedeImprimir = true;
  17. numeroCanicas = numeroCanicasx;
  18. ID = 1;
  19. }
  20.  
  21. public void letraOprimida(KeyEvent e)
  22. {
  23. if(e.getKeyCode() == 69)
  24.       System.out.println("Error de");
  25. if(e.getKeyCode() == 83)
  26.   puedeImprimir = false;
  27. }
  28.  
  29. public void run()
  30. {
  31.    Random t = new Random();
  32. for(int i = 0;i < numeroCanicas;i++)
  33. {
  34.    try
  35.    {
  36. Tiempo = (int)(t.nextDouble() * 5 + 1);
  37. this.imprimirDatos(Tiempo,ID);
  38. Tiempo*=1000;
  39. Thread.sleep(Tiempo);
  40. this.ID++;
  41.    }
  42.    catch(InterruptedException e)
  43.    {
  44.     e.printStackTrace();
  45.    }
  46. }
  47. }
  48.  
  49. public void imprimirDatos(int Tiempo,int ID)
  50. {
  51. String tiempo,id;
  52. tiempo = Integer.toString(Tiempo);
  53. id = Integer.toString(ID);
  54. areaTexto.append("ID:        "+id+"\tTiempo:        "+tiempo+"\n");
  55. }
  56. }
  57.  
  58. class Interfaz extends JFrame implements KeyListener{
  59. private JTextArea areaTexto;
  60. private JLabel etiqueta;
  61. private int numeroCanicas;
  62. private Random n = new Random();
  63. private Hilo hilo;
  64.  
  65. public Interfaz()
  66. {
  67. numeroCanicas = (int)(n.nextDouble() * 10 + 1);
  68. System.out.println(""+numeroCanicas);
  69. areaTexto = new JTextArea(20,18);
  70. etiqueta = new JLabel("Número Canicas : "+numeroCanicas);
  71. etiqueta.setBounds(10, 11, 96, 14);
  72. this.getContentPane().add(areaTexto,BorderLayout.EAST);
  73. this.getContentPane().add(etiqueta,BorderLayout.NORTH);
  74. hilo = new Hilo(areaTexto,numeroCanicas);
  75.  
  76. hilo.start();
  77. addKeyListener(this);
  78. }
  79.  
  80. public void keyTyped(KeyEvent e) {
  81. hilo.letraOprimida(e);
  82. }
  83. public void keyPressed(KeyEvent e) {
  84. }
  85. public void keyReleased(KeyEvent arg0) {
  86. }
  87. }
  88.  
  89. public class Canicas {
  90.  
  91. public static void main(String[] args) {
  92. Interfaz interfaz = new Interfaz();
  93. interfaz.setTitle("Canicas");
  94. interfaz.setBounds(200, 200, 300, 300);
  95. interfaz.setVisible(true);
  96. }
  97. }

de antemano gracias


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
keylistener
Java
H4CKO_R 3 9,183 Último mensaje 19 Mayo 2009, 17:53 pm
por Amerikano|Cls
Problema KeyListener java Linuix-->Windows
Java
smasher666 4 6,001 Último mensaje 5 Octubre 2009, 18:07 pm
por smasher666
KeyListener Problema sin modificar el código en ocasiones va y viene !!!
Java
lluvplay 1 2,854 Último mensaje 19 Abril 2012, 18:12 pm
por lluvplay
KeyListener Jpanel
Java
mapers 1 1,687 Último mensaje 28 Abril 2014, 01:26 am
por ThinkByYourself
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines