簡體   English   中英

RMI聊天室無法將數據發送到服務器

[英]rmi chat room not able to send data to server

我正在開發帶有GUI的rmi聊天室。 我必須從客戶端n接收輸入,然后將其發送到服務器。但是我無法使用從Jtextfield讀取的值。 任何人都可以幫助我如何使用用戶的讀取輸入發送到服務器

import java.applet.Applet;
import java.awt.*
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;

public class MyChatClient extends UnicastRemoteObject{

     JFrame F = new JFrame("Chat room");
 JTextField T = new JTextField(25); 
 JPanel pane = new JPanel(new GridBagLayout());
 JButton B = new JButton("submit");
 JPanel pane2 = new JPanel(new GridBagLayout());
 TextArea TA = new TextArea(15,50);
 String response;

protected  MyChatClient() throws RemoteException {


    F.setSize(400,400);
    pane.add(T);
    pane.add(B);
    F.add(pane, BorderLayout.SOUTH);

    TA.setEditable(false);
    TA.setBackground(Color.WHITE);

    pane2.add(TA);
    F.add(pane2, BorderLayout.BEFORE_FIRST_LINE);
    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    F.setVisible(true);


        B.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                response = T.getText();  // I am not able to send this to server                                                                  

             }});
    }

   public static void main(String[] args) throws RemoteException {

    MyChatClient mc = new MyChatClient();
    try{
        //System.setSecurityManager(new RMISecurityManager());
        //Registry reg = LocateRegistry.getRegistry("localhost");

       ServerImpl svr = (ServerImpl)Naming.lookup("rmi://localhost/ChatServer") ;
                    System.out.println("Server found"); 
             svr.sendMessage(response); // here is the problem

     }catch(Exception e){
        System.err.print(e);
    }

    }

 }

我認為您的動作監聽器永遠不會被調用。 請檢查這一點,

B.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                response = T.getText(); 
                System.out.println("inside action");                                                 

             }});

或添加一個按鈕並在其click事件中執行此操作。 祝好運 !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM