Abilitando teclado em JButton

2 respostas
G

pessoal tenho esta janela :

void janelaEntrada(){
			
		//criando componetentes;
			
			texto1 =  new JLabel("Login:");
			texto2 =  new JLabel("Senha:");
			texto3 =  new JLabel("Abertura de Chamados");
			campo1 =  new JTextField();
			campo2 =  new JPasswordField();
			botao1 =  new JButton("Entre");
			
			
			texto1.setBounds(50, 50, 100, 100);
			texto2.setBounds(50, 80, 100, 100);
			texto3.setBounds(60, 10, 400, 50);
			campo1.setBounds(100,90,100,20);
			campo2.setBounds(100, 120, 100, 20);
			botao1.setBounds(75, 155, 100, 20);
			

			
			
			
		//criando Janela;
			
			JPanel janelaLogin =  new JPanel();
			janelaLogin.setLayout(null);
			
			
			janelaLogin.add(texto1);
			janelaLogin.add(texto2);
			janelaLogin.add(texto3);
			janelaLogin.add(campo1);
			janelaLogin.add(campo2);
			janelaLogin.add(botao1);
			
			
			
			this.setContentPane(janelaLogin);
			this.setSize(250,250);
			setSize(250,250);
		
			setVisible(true);
			
			Verificando act =  new Verificando();
			botao1.addActionListener(act);
		
			
		}

Como eu faço para quando o usuario aperte ENTER ele disparar o evento do botão

2 Respostas

A

Você poderia usar o evento KeyPressed, mas eu recomendo que use o ActionPerformed, que trabalha não só com o teclado, mas com o mouse também:

private void botao1ActionPerformed(java.awt.event.ActionEvent evt) { // comandos }[/b]

S

Vai um exemplo básico com actionPerformed…

Classe1.java

/*
 * Classe1.java
 *
 * Created on 4 de Setembro de 2007, 17:42
 */

/**
 *
 * @author  sergio
 */
public class Classe1 extends javax.swing.JFrame {
   
    /** Creates new form Classe1 */
    public Classe1() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("Chama janela2");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1);
        jButton1.setBounds(110, 120, 150, 60);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 24));
        jLabel1.setText("Janela 1");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(130, 70, 190, 40);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Classe2().setVisible(true);
        this.setVisible(false);
    }                                       
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Classe1().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
   
}

Classe2.java

/*
 * Classe2.java
 *
 * Created on 4 de Setembro de 2007, 17:44
 */

/**
 *
 * @author  sergio
 */
public class Classe2 extends javax.swing.JFrame {
   
    /** Creates new form Classe2 */
    public Classe2() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 24));
        jLabel1.setText("Janela2");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(140, 110, 100, 30);

        jButton1.setText("Janela 1 novamente...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        getContentPane().add(jButton1);
        jButton1.setBounds(100, 150, 200, 70);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-406)/2, (screenSize.height-296)/2, 406, 296);
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            this.setVisible(false);
            new Classe1().setVisible(true);
    }                                       
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Classe2().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
   
}

Um abraço!

Criado 4 de outubro de 2007
Ultima resposta 5 de out. de 2007
Respostas 2
Participantes 3