gpt4 book ai didi

java - 如何将字符串从工作线程发送到文本区域?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:33:11 24 4
gpt4 key购买 nike

public class Client1 implements Runnable{

ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
String Buffer;
TextArea ta;
Handler mhandler;


public Client1() {
System.out.println("in constructor");
EstablishConnection();
Buffer = new String();
// mhandler = handler;
// ta = t;
}

private boolean EstablishConnection()
{


try
{
System.out.println("calling socket");
socket = new Socket("192.168.1.145",8080);
if(socket != null)
{
System.out.println("ContentApp"+ "Socket Successfully created");
}
}
catch (IOException e) {
System.out.println("ContentApp"+ "Socket IOException");
e.printStackTrace();
}
try
{
dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("ContentApp"+ "DataInputstream Successfully created");
}
catch (IOException e) {
System.out.println("ContentApp"+ "Datainputstream failed");
e.printStackTrace();
return false;
}
try
{
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF("Hi This is Hinar!");
System.out.println("ContentApp"+ "Dataoutputstream Successfully created");
}
catch (IOException e)
{
System.out.println("ContentApp"+ "Dataoutputstream failed");
e.printStackTrace();
return false;
}

if(socket != null)
{
run();


}
return true;
}


public void run() {
while(true)
{
System.out.println("ContentApp"+ "Thread is running Succesfully in loop");
try {
System.out.println("reading from socket");
Buffer = dataInputStream.readUTF();
System.out.println(Buffer+"this is the data");
Client.tarea.append(Buffer);// Text area of Frames
/*ta.setVisible(true);
ta.setText(Buffer+"this is the Buffer");*/



} catch (IOException e)
{
System.out.println("ContentApp"+ "Read IO Exception");
e.printStackTrace();
}
}

}
}

最佳答案

由于您没有发布任何异常或告诉我们到底出了什么问题,所以我只能猜测...

看看 InvokeAndWai t 或 InvokeLater SwingUtilities 类中的方法:

关于java - 如何将字符串从工作线程发送到文本区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7144358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com