更新 jTextArea 时出现问题

发布于 2024-09-28 01:52:33 字数 1997 浏览 6 评论 0原文

我正在编写一个RMI聊天程序。在我的程序中,我能够接收和发送消息,但无法在文本区域中显示它。我不确定是什么错误。我也尝试使用事件调度方法。这没有帮助。

public class client extends javax.swing.JFrame implements inter {

public client() {
    initComponents();
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        final inter i = (inter) Naming.lookup("rmi://localhost:1111/client1");
        final String msg = jTextField1.getText();
        if (msg.length() > 0) {
            jTextArea1.append("Me :" + msg);
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    try {
                        i.rcvMsg("Client 1 : " + msg);
                    } catch (RemoteException ex) {
                    }
                }
            });


        }
    } catch (RemoteException ex) {
     } catch (NotBoundException ex) {
     } catch (MalformedURLException ex) {
     }
}                                        

 public void rcvMsg(String msg) {
    final String s = msg;
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            System.out.println("server called");
            System.out.println(s);
            jTextArea1.append(s);
            System.out.println("client msg" + java.awt.EventQueue.isDispatchThread());
            jTextArea1.update(jTextArea1.getGraphics());
        }
    });
}

public static void main(String args[]) {
    try {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new client().setVisible(true);
            }
        });
        client c2 = new client();
        inter stub = (inter) UnicastRemoteObject.exportObject(c2, 0);
        Registry registry = LocateRegistry.createRegistry(1113);
        registry.bind("client2", stub);
    } catch (AlreadyBoundException ex) {
    } catch (AccessException ex) {
    } catch (RemoteException ex) {
    }
}
}

请帮忙...

I am writing a RMI chat program. In my program I am able to receive and send messages, but i am not able to display it in the TextArea. I am not sure what is the error. I tried using Event Dispatch method also. It doesn't help.

public class client extends javax.swing.JFrame implements inter {

public client() {
    initComponents();
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        final inter i = (inter) Naming.lookup("rmi://localhost:1111/client1");
        final String msg = jTextField1.getText();
        if (msg.length() > 0) {
            jTextArea1.append("Me :" + msg);
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    try {
                        i.rcvMsg("Client 1 : " + msg);
                    } catch (RemoteException ex) {
                    }
                }
            });


        }
    } catch (RemoteException ex) {
     } catch (NotBoundException ex) {
     } catch (MalformedURLException ex) {
     }
}                                        

 public void rcvMsg(String msg) {
    final String s = msg;
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            System.out.println("server called");
            System.out.println(s);
            jTextArea1.append(s);
            System.out.println("client msg" + java.awt.EventQueue.isDispatchThread());
            jTextArea1.update(jTextArea1.getGraphics());
        }
    });
}

public static void main(String args[]) {
    try {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new client().setVisible(true);
            }
        });
        client c2 = new client();
        inter stub = (inter) UnicastRemoteObject.exportObject(c2, 0);
        Registry registry = LocateRegistry.createRegistry(1113);
        registry.bind("client2", stub);
    } catch (AlreadyBoundException ex) {
    } catch (AccessException ex) {
    } catch (RemoteException ex) {
    }
}
}

Please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜味拾荒者 2024-10-05 01:52:33

仅仅使用 getGraphics() 共享一些信息是不受欢迎的,并且可能会导致问题,

jTextArea1.update(jTextArea1.getGraphics());

我还使用 RMI 创建了聊天应用程序:

RMI 中的传递引用问题? 那里也写了客户端,也许对你有用。

just sharing some information using getGraphics() is not appreciated and can cause problems,

jTextArea1.update(jTextArea1.getGraphics());

and i have also created chat application with RMI:

Pass by reference problem in RMI? there is also client written over there, may be that would be useful for you.

就此别过 2024-10-05 01:52:33

main中创建c2后,调用c2.setVisible(true);

正在调用rcvMsg中的代码clientc2 实例。由于 c2 实例永远不可见,因此您看不到任何变化。

您可能希望客户端连接到服务器,而不是直接连接到另一个客户端。客户端到客户端将适用于 2 个端点。但如果你想添加第三个,会发生什么?第四?您确实需要一个服务器来充当所有客户端的中介。

In main after creating c2, call c2.setVisible(true);

The code in rcvMsg is being called on the c2 instance of client. Since the c2 instance is never made visible, you see no change.

You probably want a client to connect to a server, not directly to another client. The client-to-client will work for 2 endpoints. But what happens if you want to add a third? A forth? You really want a server that will act as an intermediary for all the clients.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文