床头掉皮解决图:在JAVA服务器端文本框显示客户端信息问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/01 16:33:49
偶想在服务器端界面的文本框上显示客户端发来的信息

//服务器端类部分代码
public class NetBarServerFrame extends JFrame {
JTextArea txaInformation = new JTextArea();

//接收信息并显示在JTextArea的对象上
void ReceiveInformation(String Information){
System.out.println(Information);
txaInformation.append(Information);
}

//一个菜单选项,用作启动线程
public void jMenuItem1_actionPerformed(ActionEvent e) {
new NetBarServerThread().start();
}
}

//线程类
public class NetBarServerThread extends Thread{

protected Socket s;
NetBarServerFrame NetFrame=new NetBarServerFrame();

public void run(){
try{
while(true){
ServerSocket ss = new ServerSocket(6000);
Socket s=ss.accept();
OutputStream sendInformation = s.getOutputStream();
sendInformation.write("Welcome".getBytes());
InputStream RecviedInformation=s.getInputStream();
byte[] buf=new byte[20];
RecviedInformation.read(buf);
//调用服务器端类的ReceiveInfomation类并传递buf参数
NetFrame.ReceiveInformation(new String(buf));
System.out.println(new String(buf));
s.close();
ss.close();
sendInformation.close();
RecviedInformation.close();
}
}catch(Exception e){
............
}
}
}

现在主要问题就是``当线程类调用ReceiveInformation()方法时,只执行System.out.println(Information);,而不执行txaInformation.append(Information);就是不能把信息显示在文本框上,试过把append()方法改为setText()情况也是一样,也试过在线程类(NetBarServerThread)的死循环里写入NetFrame.txaInformation.setText(new String(buf));文本框依然没有显示信息,现在很迷惘,搞了一整天都不知道错了哪里,希望哪位高人能指点以下.