0

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

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) {
    }
}
}

请帮忙...

4

2 回答 2

1

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

jTextArea1.update(jTextArea1.getGraphics());

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

RMI 中的通过引用问题?那里也有写客户端,可能对你有用。

于 2010-10-18T21:18:29.123 回答
0

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

中的代码在.c2实例rcvMsg上被调用。由于 c2 实例从不可见,因此您看不到任何变化。client

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

于 2010-10-18T19:49:19.870 回答