Pessoal,
Alguém pode me explicar como funciona, a captura de eventos por teclado utilizando KeyAdapter?
Para cada, evento capturado por teclado será necessário a criação de um novo método?
public void keyTyped(KeyEvent e) {
teclado_keyTyped(e);
}
Agradeço a todos...
Trajano
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ExemploSair extends JFrame implements ActionListener {
private JButton botaoSair;
public ExemploSair(String title) {
super(title);
botaoSair = new JButton("Sair");
botaoSair.setToolTipText("Clique para sair");
botaoSair.addActionListener(this);
//A chamada desde método esta correta?
botaoSair.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
teclado_keyTyped(e);
}
});}
private void criaMostraGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
JLabel label = new JLabel("Clique no botão para sair.");
JPanel panel = new JPanel();
panel.add(label);
getContentPane().add(panel);
JPanel panel2 = new JPanel();
panel2.add(botaoSair);
getContentPane().add(panel2);
setLocation(300, 300);
pack();
setVisible(true);
}
//Eventos do teclado
public void teclado_keyTyped(KeyEvent e) {
if (e.getSource() == botaoSair) {
System.exit(0);
}
}
//Eventos do Mouse
public void actionPerformed(ActionEvent e) {
if (e.getSource() == botaoSair) {
System.exit(0);
}
}
public static void main(String[] args) {
ExemploSair exemploSair = new ExemploSair("Exemplo Sair");
exemploSair.criaMostraGUI();
}
}