bom dia ,
tenho um programa e gostaria de colocar teclas de atalho para os botoes existentes , tipo F1 , F2 … etc.
coloquei o KeyEvent , mas nao ta dando certo
alguem pode me ajudar?
ai vai o codigo :
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
import br.com.sweetmellon.telas.Grupo;
public class SweetMellon extends JFrame implements KeyListener{
//private JFrame frame_principal;
private JPanel principal;
private JPanel topo;
private JButton clientes;
private JButton pedidos;
private JButton grupos;
private JButton produtos;
private JButton sair;
private Grupo tela_grupo;
public SweetMellon(){
//frame_principal = new JFrame("SweetMellon");
super("Sweet Mellon");
principal = new JPanel();
topo = new JPanel();
clientes = new JButton("Clientes");
pedidos = new JButton("Pedidos");
grupos = new JButton("Grupos");
produtos = new JButton("Produtos");
sair = new JButton("Sair");
/*ACTIONS */
grupos.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new Grupo();
//tela_grupo.setVisible(true);
}
});
sair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sair();
}
});
//sair.setMnemonic(KeyEvent.VK_F12);
topo.add(clientes);
topo.add(pedidos);
topo.add(grupos);
topo.add(produtos);
topo.add(sair);
principal.setBackground(Color.LIGHT_GRAY);
principal.setLayout(new BorderLayout());
principal.add(topo,BorderLayout.NORTH);
this.setSize(800,600);
this.getContentPane().add(principal);
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void sair(){
System.exit(0);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
SweetMellon s = new SweetMellon();
}
public void keyTyped(KeyEvent ke){
}
public void keyPressed(KeyEvent ke){
if(ke.getKeyCode() == KeyEvent.VK_F12){
//System.out.println("F12");
sair();
}
}
public void keyReleased(KeyEvent ke){
}
}