문제

Hey i just started with Java, RMI and JFrame about 2 weeks ago. I'm doing a game, it works fine as a console but now i'm adding a GUI and i'm getting confused about how to get the Remote Client messages to my Text Area

Remote Client Interface

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;
}

Remote Client

    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");
        }
    }

}

In my program without any GUI, for example when a player login (it sends a message to all players with that notification)

for (Player player : serverObject.getPlayers()) 
                  {
                     player.getClient().notifyLogin(username);
                  }

But now i don't want that text in the System.out, i want it in a text area

enter image description here

So can anyone make me a description about how to do this?Code is not the most important, i just want to understand how this works from a remote client to GUI (JTextArea)?

도움이 되었습니까?

해결책

I'm not sure exactly what you have already. If the GUI in the picture is already built and you have your JTextArea object created, then just replace the line System.out.println(...) with myTextArea.setText(...) or myTextArea.append(...) depending if you want to keep previous content or replace it.

다른 팁

With my understanding you would have to add a button listener to a button (or a mouse listener for the text box) that would look for a certain keystroke (maybe the enter button) or you could even add a JButton to your GUI that the user would click to "submit" their text in that text area.

In the button listener you would just write something like JTextArea.getText(); (the "JTextArea" would just be whatever you called the text area variable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top