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

发布于 2024-11-30 21:15:20 字数 2542 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2024-12-07 21:15:20

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

看看 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:

各空 2024-12-07 21:15:20

请阅读有关Swing 中的并发性的教程,然后添加此代码, 例如

Runnable doRun1 = new Runnable() {

    @Override
    public void run() {
        ta.setText(Buffer+"this is the Buffer");
        ta.setVisible(true);
    }
};
SwingUtilities.invokeLater(doRun1);

please reads tutorial about Concurency in Swing, then just add this code, for example

Runnable doRun1 = new Runnable() {

    @Override
    public void run() {
        ta.setText(Buffer+"this is the Buffer");
        ta.setVisible(true);
    }
};
SwingUtilities.invokeLater(doRun1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文