嘿,我大约两周前才开始使用 Java、RMI 和 JFrame。 我正在做一个游戏,它作为控制台工作得很好,但现在我正在添加一个 GUI,我对如何将远程客户端消息发送到我的文本区域感到困惑
远程客户端界面
public interface IRemoteClientObject extends Remote {
public void notifyLogin(String player) throws RemoteException;
public void notifyLogout(String player) throws RemoteException;
public void notifyStatus(String player, PlayerStatus status) throws RemoteException;
public boolean challenge(String player) throws RemoteException;
}
远程客户端
public class RemoteClientObject extends UnicastRemoteObject implements
IRemoteClientObject {
/**
*
*/
private static final long serialVersionUID = -7209335248515276286L;
/**
* @throws RemoteException
*/
public RemoteClientObject() throws RemoteException {
}
@Override
public void notifyLogin(String player) throws RemoteException {
System.out.println("\n" + player + " joined the game");
}
@Override
public void notifyLogout(String player) throws RemoteException {
System.out.println("\n" + player + " left the game");
}
@Override
public void notifyStatus(String player, PlayerStatus status) throws RemoteException {
if (status.equals(PlayerStatus.PLAYING))
{
System.out.println("\n" + player + " is now playing");
}
else if (status.equals(PlayerStatus.READY))
{
System.out.println("\n" + player + " is available to play");
}
else
{
System.out.println("\n" + player + " is unavailable to play");
}
}
}
在我的程序中,没有任何 GUI,例如当玩家登录时(它会向所有玩家发送一 strip 有该通知的消息)
for (Player player : serverObject.getPlayers())
{
player.getClient().notifyLogin(username);
}
但现在我不希望该文本出现在 System.out 中,我希望它出现在文本区域中
那么有人可以给我一个关于如何做到这一点的描述吗?代码不是最重要的,我只是想了解它如何从远程客户端到 GUI(JTextArea)工作?
最佳答案
我不确定你已经拥有什么。如果图片中的 GUI 已构建并且您已创建 JTextArea 对象,则只需将 System.out.println(...)
行替换为 myTextArea.setText(... )
或 myTextArea.append(...)
取决于您是要保留以前的内容还是替换它。
关于Java RMI 与 JFrame - 从客户端获取文本到 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22620023/