Java rmi聊天室无法向服务器发送数据
我正在开发一个带有GUI的rmi聊天室。我必须从客户端获取输入并将其发送到服务器。但我无法使用从Jtextfield读取的值。有谁能帮助我如何使用用户的读取输入发送到服务器Java rmi聊天室无法向服务器发送数据,java,Java,我正在开发一个带有GUI的rmi聊天室。我必须从客户端获取输入并将其发送到服务器。但我无法使用从Jtextfield读取的值。有谁能帮助我如何使用用户的读取输入发送到服务器 import java.applet.Applet; import java.awt.* import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import javax.swin
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事件中执行此操作。祝你好运 看起来您是在问如何延迟调用
sendMessage
,直到调用actionPerformed
之后。这就是你的问题所在,还是我误解了?即使是tat也会发问,但我必须从textfield中阅读并将其发送到服务器这里的问题是什么?如何从JTextField中获取数据?我需要将在textfield中输入的数据保存到一个变量中,然后发送到服务器,问题是?