Título: Duda Eventos teclas F1, F2, F3. etc
Publicado por: ZedGe en 18 Noviembre 2011, 00:47 am
Bueno me estoy iniciando en Swing y quisiera hacer lo siguiente... tengo un botón que al ser presionado cierra el frame public void actionPerformed(ActionEvent e) { if (e.getSource()==BotonEscape) { System.exit(0); } } Lo que necesito es un código que al apretar la tecla F1 haga automáticamente ese evento, o si creo un botón de imprimir, que al apretar el F2 imprima automáticamente Es para comodidad crear estos atajos de teclado, es decir, que si presiono el boton o F1 realize la misma accion. Logre hacerlo con otras teclas pero con estas "Especiales" no. Encontré este código import java.awt.*; import java.applet.Applet; import java.awt.event.KeyEvent; import java.awt.event.KeyListener;
public class asdasdasd extends Applet { Label lb1; TextField t1;
public void init () { t1 = new TextField(20); add(t1);
lb1 = new Label(" "); add(lb1);
t1.addKeyListener(new KeyListener() { int perro = 10; char caracter; String cadena; public void keyTyped(KeyEvent eve) { caracter = eve.getKeyChar(); cadena = String.valueOf(caracter); lb1.setText(cadena); }
public void keyPressed(KeyEvent eve) { if (eve.getKeyCode() == KeyEvent.VK_ALT){ cadena = "alt";}
if (eve.getKeyCode() == KeyEvent.VK_F1) cadena = "F1"; if (eve.getKeyCode() == KeyEvent.VK_F2) cadena = "F2"; lb1.setText(cadena);
}
public void keyReleased(KeyEvent eve) {} }); } } El problema es que debe ir si o si el textfield para que pueda leer la tecla.... alguien me podria ayudar?
Título: Re: Duda Eventos teclas F1, F2, F3. etc
Publicado por: ZedGe en 18 Noviembre 2011, 01:11 am
Este es mi código, lo he puesto pero no me funciona :S /*
package puntodeventa;
import java.awt.event.*; import javax.swing.*;
public class Prueba extends JFrame implements ActionListener { JButton botonVentaDeContado; public Prueba() { setLayout(null); botonVentaDeContado=new JButton("Consultar Producto"); botonVentaDeContado.setBounds(300,250,100,30); add(botonVentaDeContado); botonVentaDeContado.addActionListener(this); addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent arg0) {} @Override public void keyReleased(KeyEvent arg0) {} @Override public void keyPressed(KeyEvent event) { if(event.getKeyCode() == KeyEvent.VK_F1) System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if (e.getSource()==botonVentaDeContado) { System.exit(0); } } public static void main(String[] ar) { Prueba formulario1=new Prueba(); formulario1.setBounds(0,0,450,350); formulario1.setVisible(true); } }
Título: Re: Duda Eventos teclas F1, F2, F3. etc
Publicado por: ZedGe en 18 Noviembre 2011, 01:17 am
me faltaba ponerle lo siguiente botonVentaDeContado.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent arg0) {} @Override public void keyReleased(KeyEvent arg0) {} @Override public void keyPressed(KeyEvent event) { if(event.getKeyCode() == KeyEvent.VK_F1) System.exit(0); } });
Título: Re: Duda Eventos teclas F1, F2, F3. etc
Publicado por: Leyer en 18 Noviembre 2011, 01:38 am
Haber eso no funcionara si cambias el focus a otro componente usando el KeyEventDispatcher import java.awt.FlowLayout; import java.awt.KeyEventDispatcher; import java.awt.KeyboardFocusManager; import java.awt.event.*; import javax.swing.*; private static final long serialVersionUID = 1L; public Prueba() { botonVentaDeContado =new JButton("Consultar Producto"); botonVentaDeContado.setBounds(300,250,100,30); add(botonVentaDeContado); botonVentaDeContado.addActionListener(this); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); } if (e. getSource()==botonVentaDeContado ) System. exit(0); } public static void main (String[] ar ) { Prueba formulario1=new Prueba(); formulario1.setBounds(0,0,450,350); formulario1.setVisible(true); } @Override public boolean dispatchKeyEvent (KeyEvent arg0 ) { return false; } }
Título: Re: Duda Eventos teclas F1, F2, F3. etc
Publicado por: ZedGe en 18 Noviembre 2011, 21:07 pm
Y cual seria la diferencia entre los 2 metodos?? aun que son algo parecidos
|