Galera, eu tô fazendo um chat e queria que quando a pessoa apertasse o “Enter” uma mensagem seja enviada ao servidor… :-o
public class ChatCliente extends JFrame{
JTextField textoParaEnviar;
Socket socket;
PrintWriter escritor;
String nome;
JTextArea textoRecebido;
Scanner leitor;
JScrollPane scroll;
private class EscutaServidor implements Runnable{
public void run() {
try {
String texto;
while ((texto = leitor.nextLine()) != null) {
textoRecebido.append(texto + "\n");
scroll.getVerticalScrollBar().setValue(textoRecebido.getHeight());
}
} catch (Exception e) {}
}
}
public ChatCliente(String nome) {
super("Protótipo - Chat : " + nome);
this.nome = nome;
Container envio = new JPanel();
envio.setFocusable(true);
envio.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
enviarTextoParaServidor();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
envio.setLayout(new BorderLayout());
Font fonte = new Font("Serif", Font.PLAIN, 26);
textoParaEnviar = new JTextField();
textoParaEnviar.setFont(fonte);
JButton botao = new JButton("Enviar");
//botao.setFocusable(true);
//botao.addKeyListener(new EventoTecladoEnviar()); // já tentei usar o evento no botao com uma classe privada
botao.setFont(fonte);
botao.addActionListener(new EnviarListener());
envio.add(BorderLayout.CENTER, textoParaEnviar);
envio.add(BorderLayout.EAST, botao);
//textarea
textoRecebido = new JTextArea();
textoRecebido.setFont(fonte);
textoRecebido.setEditable(false);
textoRecebido.setLineWrap(true);
scroll = new JScrollPane(textoRecebido);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(BorderLayout.CENTER, scroll);
getContentPane().add(BorderLayout.SOUTH, envio);
configurarRede();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
textoParaEnviar.grabFocus();
}
private class EnviarListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
enviarTextoParaServidor();
}
}
public void enviarTextoParaServidor() {
escritor.println(nome + " : " + textoParaEnviar.getText());
escritor.flush();
textoParaEnviar.setText("");
textoParaEnviar.requestFocus();
}
private void configurarRede() {
try{
socket = new Socket("127.0.0.1", 5000);
escritor = new PrintWriter(socket.getOutputStream());
leitor = new Scanner(socket.getInputStream());
new Thread(new EscutaServidor()).start();
escritor.println(this.nome + " entrou na sala");
escritor.flush();
}catch(Exception e){
}
}
public static void main(String[] args) {
String nome = JOptionPane.showInputDialog("Qual seu nome?");
new ChatCliente(nome);
}
}
Já tentei muita coisa: usar o KeyListener no JFrame, JPanel, JButton bla bla… pesquisei aqui no GUJ… O programa funciona mas não consigo implmeentar essa função do enter nem… ou melhor não consigo manipular ação de teclado com nenhuma tecla… :twisted: