1

我正在实现一个二十一点游戏,我试图允许多个类(分别为 ChatGUI 和 GameGUI)使用我的“客户端”类,它实际上是服务器的入口。

我尝试使用 Client 类作为每个构造函数的参数来创建这些类,但似乎我的 Client 类无法将信息发送回其他类 - ChatGUI/GameGUI。

我已经有了一个可以处理多个客户端连接的工作服务器!(经过试验和测试)。

public class BlackJack{
    private Client client;
    private ChatGUI chatgui;
    private GameGUI gamegui;

    public BlackJack(){
        // Setup Client class, which will be passed to all other classes
        client = new Client(server, port, username, chatgui, gamegui);

        // Setup other classes, which will be given Client class
        chatgui = new ChatGUI(client);
        gamegui = new GameGUI(client); 

    }   
}

public class Client{
    private ChatGUI chatgui;
    private GameGUI gamegui;

    Client(String server, int port, String username, ChatGUI cg, GameGUI gamegui){
        // catch these arguments and assign them to variables     
    }

    void display(String msg){
        // Method to display incoming messages to the chat screen
        // Using ChatGUI method (*** not working? ***)
        chatgui.append(msg + "\n");
    }
}

public class ChatGUI{
    private JTextArea textarea;
    private Client client;

    public ChatGUI(Client c){
        client = c;
    }

    // ChatGUI can use client methods
    void sendMessage(String msg){
        client.sendChat(msg);
     }

    void append(String msg){
        textarea.append(msg);
}

public class GameGUI{
    private Client client;

    public GameGUI(Client c){
        client = c;
    }

    // GameGUI can use client methods
    void playGame(){
        client.playGame();
    }
}

请注意,此代码是为更多的伪代码参考而编写的 - 不想粘贴数百行。

任何帮助将非常感激!非常感谢您的阅读。

4

3 回答 3

2

您正在将 chatGUI 的引用传递给您的客户端类,然后在这样做之后实际实例化 chatGUI。所以之前传递的引用是不正确的。您需要首先实例化 chatGUI 和 gameGUI 对象。然后创建客户端对象并正确设置引用。不要尝试在构造函数中传递引用,因为那时它们是无效的。

于 2013-04-24T16:06:45.883 回答
2

假设我正确理解了您的问题,我认为对您的课程结构有所不同会有所帮助...

每个 BlackJack 对象都有一个客户端,例如

public class BlackJack{
    private Client client;

    public BlackJack(){
        // Setup Client class, which will be passed to all other classes
        client = new Client(server, port, username);

    }  

然后每个客户端对象都有一个 GUI 类的实例,例如

public class Client{
    private ChatGUI chatgui;
    private GameGUI gamegui;

    Client(String server, int port, String username){
             chatgui = new ChatGUI(this);
             gamegui = new GameGUI(this);
    }

    //handle messages from server
    void onMessageRecieved(String msg){
            if(/* the message pertains to the game gui */ )
                    gamegui.newMessage(msg);
            else if( /* the message pertains to the chat gui */ )
                    chatgui.newMessage(msg);
    }

    //here you can add functions for the gui classes to call
    public void sendChat(String chat){

    }
}

那么你的 gui 类可能看起来像......

public class ChatGUI{
    private JTextArea textarea;
    private Client client;

    public ChatGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }

    public void sendChat(String msg){
        client.sendChat(msg);
    }
}

public class GameGUI{
    private Client client;

    public GameGUI(Client c){
        client = c;
    }

    //receive message/command from server
    public void newMessage(String msg){
        //perform the desired action based on the command
    }
}
于 2013-04-24T16:07:37.420 回答
0

嗯,我想知道我是否理解正确,但是您想要的是:Client1 向 Client2 发送聊天消息吗?

换句话说:chat1-->Client1-->client2-->gui2

如果这是正确的,那么消息如何传输到 client2 ?

于 2013-04-24T15:55:47.187 回答