Java RMI 与 JFrame - 从客户端获取文本到 GUI

标签 java swing user-interface rmi

嘿,我大约两周前才开始使用 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 中,我希望它出现在文本区域中

enter image description here

那么有人可以给我一个关于如何做到这一点的描述吗?代码不是最重要的,我只是想了解它如何从远程客户端到 GUI(JTextArea)工作?

最佳答案

我不确定你已经拥有什么。如果图片中的 GUI 已构建并且您已创建 JTextArea 对象,则只需将 System.out.println(...) 行替换为 myTextArea.setText(... )myTextArea.append(...) 取决于您是要保留以前的内容还是替换它。

关于Java RMI 与 JFrame - 从客户端获取文本到 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22620023/

相关文章:

java - 运行 import javax.swing.JOptionpane 时出错

user-interface - Walk GUI Toolkit for GO 中的文件选择器组件

javascript - 如何将博客中的标签<br/>更改为<p>?

java - 比较两个列表并从一个列表中删除重复项

java - 如何在可变参数中测试 arg

java - 如何使用 Maven 运行特定包中的所有测试?

Java:如何在打开另一个 JFrame 时关闭它?

java - JPanel 如何显示颜色数组中的不同颜色?

python - 在 Tkinter 中使用 askdirectory() 选择文件夹时显示所有文件

java - 如何在配置中设置爆炸威力?