Assuming that you have a class called ManejadorEventos
and that you want to manipulate in this the events that are generated in class Ventana
. First you must have in the class ManejadorEventos
methods that receive as parameters objects of the type of event that will handle.
public class ManejadorEventos {
public void manejarEventoTeclado(KeyEvent event) {
// aquí irá el código necesario
}
}
Then you must have an instance of this class in class Ventana
and invoke the methods you want by passing them the event objects.
public class Ventana extends JFrame implements KeyListener {
ManejadorEventos manejador = new ManejadorEventos();
// Otros atributos
@Override
public void keyPressed(KeyEvent e) {
manejador.manejarEventoTeclado(e);
}
// Otros métodos
}
Note that I have created the instance of the event handler class in the same declaration of the attribute, but you could pass the correct instance using the constructor of class Ventana
.