Bem básico, mas não funciona

2 respostas
R

criei este programa só para testar se o usuário teclou enter e mostrar a
mensagem mas não funciona!!!

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.*;

public class Swing12 extends JFrame {
private JTextField nome;

Container form;

public void Monta_Form(){
  form = new JFrame("Teste de Eventos");
  form.setLocation(10,10);
  form.setSize(800,600);
  form.setVisible(true);
		
  JLabel rotulo1 = new JLabel("Nome:"); 
  form.add(rotulo1); 
  rotulo1.setBounds(10,5,200,20);
		
  JTextField nome = new JTextField("",10);
  form.add(nome);
  nome.setBounds(80,5,100,20);
		
  TrataEdit tratat;
  tratat = new TrataEdit();

  nome.addActionListener(tratat);    
}

public static void main(String[] args) {
  new Swing12().Monta_Form();

}

private class TrataEdit implements ActionListener {

public void actionPerformed (ActionEvent evento){
  
  if (evento.getSource() == nome) 
    JOptionPane.showMessageDialog(null, "você teclou enter", "Mensagem", JOptionPane.INFORMATION_MESSAGE);
}

}
}

2 Respostas

R

Ao invés de você utilizar ActionListener, utilize KeyListener. Então você poderá capturar a tecla pressionada. Verifique se foi a tecla enter e então mostre a mensagem.

Falou.

R

ok, valeu

Criado 26 de abril de 2007
Ultima resposta 26 de abr. de 2007
Respostas 2
Participantes 2