就是这个client端得这个代码。在新的Thread里面接收服务器传回来的字符串,能够在后台打印出来,但是就是不能在textArea里面打印出来,不知道是为什么,也没有报错?
那位高手给指点下?
client端代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;public class ChatClient extends Frame { Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
TextField tf = new TextField();
TextArea ta = new TextArea();
private boolean bconnected = false; public static void main(String[] args) {
new ChatClient().launchFrame(); } public void launchFrame() {
setLocation(300, 300);
setSize(400, 500); final TextField tf = new TextField();
final TextArea ta = new TextArea(); this.add(tf, BorderLayout.SOUTH);
this.add(ta, BorderLayout.NORTH);

this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
System.exit(0);
} });
tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
TextField t1 = (TextField) e.getSource();
String str = t1.getText().trim();
// ta.setText(str);
// OutputStream os=null;
tf.setText("");
try {
dos.writeUTF(str);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} try {
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} });
pack(); setVisible(true);
connect();
} public void deconnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
bconnected = true;
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
new Thread(new RecevThread()).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private class RecevThread implements Runnable { public void run() {
try {
while (bconnected) {
String str = dis.readUTF();
//System.out.println("i accept!");
ta.setText("nihao a  hhe");
//ta.sett
 System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
} }}server端代码:
import java.io.*;
import java.net.*;
import java.util.*;public class ChatServer {
boolean started = false;
ServerSocket ss = null;
//DataInputStream dis = null;
Socket s = null;
List<Client> clients = new LinkedList<Client>(); public static void main(String[] args) {
new ChatServer().start();
} public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("请关掉相关程序!");
System.exit(0);
} try {
while (started) {
s = ss.accept();
Client c = new Client(s);

new Thread(c).start();
clients.add(c);

}catch (IOException e) {
e.printStackTrace();
}
} class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean connected = false;
//String str = ""; Client(Socket s) {
this.s = s;
System.out.println("A client connect!");
InputStream is = null;
OutputStream os =null;
try {
is = s.getInputStream();
os =s.getOutputStream();
connected = true;
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
//str =dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
} }
public void send(String str){
try {
dos.writeUTF(str);
//dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
try {
while (connected) {
String s = dis.readUTF();
System.out.println(s);
for(int i=0;i<clients.size();i++){
Client c = clients.get(i);
c.send(s);
}
}
} catch (IOException e) {
System.out.println("deconnect now!");
} finally {
try {
if (dis != null)
dis.close();
if(dos != null)
dos.close();
if (s != null)
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}}

解决方案 »

  1.   

    ta, tf 是ChatClient的成员变量,在launchFrame中不应该再声明和创建,将launchFrame中的如下两行去掉即可:final TextField tf = new TextField(); 
    final TextArea ta = new TextArea();
      

  2.   

    把那两行代码去掉了,但是运行后还是不能将输入的字符显示在textArea里面。注:我这里是实现QQ群聊的功能,就是对方输入的内容能够通过socket接受并显示到我的textArea里面狂等