
Ola a todos! meu problema é o seguinte, escrevi um programa simples de mensagens com sockets. No arquivo Client.java ha um o metodo runClient() que é chamado no metodo main iniciando a conexao com o servidor na execucao do programa, ate ai tudo bem, porem quando tendo associar o metodo runClient() ha um evento de um JButton definido dentro do Construtor nao funciona corretamente. Ao clicar no botao ele se conecta ao servidor mas o JButton nao retorna ao seu estado inicial, ele fica pressionado. segue o codigo iniciado :
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
public class Client extends JFrame
{
private JTextField texto;
private JTextArea display;
private JButton conectar;
ObjectOutputStream saida;
ObjectInputStream entrada;
UIManager.LookAndFeelInfo looks[];
//****************************************************************
public Client()
{
super("Cliente");
looks = UIManager.getInstalledLookAndFeels();
try
{
UIManager.setLookAndFeel( looks[ 0 ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
catch ( Exception e )
{
e.printStackTrace();
}
Container c = getContentPane();
JPanel p = new JPanel(new GridLayout(1,2));
texto = new JTextField();
texto.setEnabled(false);
//texto.setForeground(color.BLUE)
texto.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendData(e.getActionCommand());
}
}
);
///PROBLEMA ESTA QUANDO CHAMA O METODO ASSIM
conectar = new JButton("Conectar");
conectar.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
runClient();
}
}
);
p.add(conectar);
p.add(texto);
c.add( p, BorderLayout.NORTH);
display = new JTextArea();
c.add(new JScrollPane(display), BorderLayout.CENTER);
setSize(400,250);
show();
}
//****************************************************************
public void runClient()
{
Socket cliente;
try
{
display.setText("Cliente conectando
");
cliente = new Socket(InetAddress.getByName("127.0.0.1"),5000);
display.append("Conectado ao " + cliente.getInetAddress().getHostName());
saida = new ObjectOutputStream(
cliente.getOutputStream());
saida.flush();
entrada = new ObjectInputStream(
cliente.getInputStream());
display.append("
I/O Streams foi pega
");
texto.setEnabled(true);
String message = "";
do
{
try
{
message = (String) entrada.readObject();
display.append("
" + message);
display.setCaretPosition(display.getText().length());
}
catch(ClassNotFoundException cnfex)
{
display.append("
Tipo de Objeto recebido Desconhecido");
}
}while(!message.equals("SERVIDOR>> TERMINAR"));
display.append("
Fechando conexao
");
saida.close();
entrada.close();
cliente.close();
}
catch(EOFException eof)
{
System.out.println("Servidor terminou conexao");
}
catch(IOException io)
{
io.printStackTrace();
}
}
//****************************************************************
private void sendData(String s)
{
try
{
saida.writeObject("CLIENTE>> " + s);
saida.flush();
display.append("
CLIENTE>> " + s );
texto.setText("");
}
catch(IOException cnfex)
{
display.append("
Erro ao escrever o objeto") ;
}
}
//****************************************************************
public static void main(String args[])
{
Client app = new Client();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
//QUANDO E CHAMADO DESTE FORMA NAO HA PROBLEMA
// app.runClient();
}
}
Leila muito obrigado