聊天 TCP 上的空白 Jframe 负载 [英] Blank Jframe load on chat TCP

查看:30
本文介绍了聊天 TCP 上的空白 Jframe 负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一个聊天TCP.如果我独立运行文件服务器和客户端,就可以了.但是如果我使用其他 jframe 运行,它有主机按钮来运行服务器,它会显示一个空白的 jframe

i try to write a chat TCP. if i run file server and client independently, it is ok. But if i run by use other jframe, which has host button to run server, it will show a blank jframe

{
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        FrmServer f=new FrmServer();
        f.setVisible(true);
    } 
}

这是 FrmServer

this is FrmServer

 public FrmServer() throws HeadlessException {
    setTitle("Server");
    setLayout(new BorderLayout());

    addContent();
    addSend();

    setSize(400, 300);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    chat();
}
private ServerSocket serverInfo;
private Socket clientInfo;
private ObjectInputStream ois;
private ObjectOutputStream oos;

private void chat() {
    try {
        serverInfo = new ServerSocket(12345);

        clientInfo = serverInfo.accept();
        ois = new ObjectInputStream(clientInfo.getInputStream());

        oos = new ObjectOutputStream(clientInfo.getOutputStream());

        while (true) {
            String data = ois.readObject().toString();

            txtContent.append("Client :" + data + "\n");
        }
    } catch (Exception ex) {
    }
}

推荐答案

以下是使用新线程修改后的代码(虽然没试过):

Here is the modified code using a new thread (though not tried):

 public FrmServer() throws HeadlessException {
    setTitle("Server");
    setLayout(new BorderLayout());

    addContent();
    addSend();

    setSize(400, 300);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    chat();
}
private ServerSocket serverInfo;
private Socket clientInfo;
private ObjectInputStream ois;
private ObjectOutputStream oos;

private void chat() {
    Runnable runnable = new Runnable() {
        public void run() {
            try {
                serverInfo = new ServerSocket(12345);

                clientInfo = serverInfo.accept();
                ois = new ObjectInputStream(clientInfo.getInputStream());

                oos = new ObjectOutputStream(clientInfo.getOutputStream());

                while (true) {
                    String data = ois.readObject().toString();
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run(){
                            txtContent.append("Client :" + data + "\n");
                        }
                    });
                }
            } catch (Exception ex) {
            }

        }
    };
    //You can use a ThreadPool too.
    new Thread(runnable).start();
}

使用 Java 8,您可以改进语法.

With Java 8 you can improve the syntax.

重要的是你不应该在 Swing/AWT 事件调度线程上做任何耗时的事情.连接到服务器,等待回复肯定很耗时.尽管问题是,您无法从其他线程更新 UI.所以你需要 SwingUtilities 类来在 EDT 上执行代码.

The important thing is you should not do anything time consuming on the Swing/AWT Event Dispatch Thread. Connecting to a server, waiting for reply certainly time consuming. Though the catch is, you cannot update the UI from other threads. So you need the SwingUtilities class to execute code on EDT.

Swing 中的并发 教程可能有助于理解细节.

Concurrency in Swing tutorial might help understanding the details.

这篇关于聊天 TCP 上的空白 Jframe 负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆