如何将字符串从工作线程发送到文本区域?
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();
}
}
}
}
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();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您没有发布任何异常或告诉我们实际出了什么问题,我只能猜测...
看看 InvokeAndWait 或 InvokeLater SwingUtilities 类中的方法:
Since you didn't post any exception or tell us what actually goes wrong, I can only guess...
Have a look at the InvokeAndWait or InvokeLater methods in the SwingUtilities class:
请阅读有关Swing 中的并发性的教程,然后添加此代码, 例如
please reads tutorial about Concurency in Swing, then just add this code, for example