2013-04-24 194 views
1

我實現一個21點遊戲,我試圖讓多個類 - (分別ChatGUI和GameGUI)用我的「客戶」類,這是有效的門口到服務器。共享對象

我試着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(); 
    } 
} 

請注意,此代碼是針對更多僞代碼引用編寫的 - 不想粘貼數百行代碼。

任何幫助將不勝感激!非常感謝您的閱讀。

+0

從服務器類/方法單獨的線程上運行的客戶端類/方法?如果沒有,那麼你的服務器將不會從客戶端得到任何數據,而不明確地調用它們 – 2013-04-24 15:38:57

回答

2

假設我正確理解你的問題,我認爲這將是有益的你構成你的類有點不同......

每個酒杯對象都有一個客戶端,例如

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

    } 

然後,每個Client對象都有一個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 
    } 
} 
+0

非常好,我會放棄它,謝謝! :) – mason 2013-04-24 16:11:45

+0

工作很好,謝謝喬爾 – mason 2013-04-28 14:41:20

0

哼,我不知道如果我理解正確的,但它是什麼你想要的是:客戶端1發送聊天信息給客戶機2?

換句話說:客服1 - >客戶端1 - >客戶機程序 - > GUI2

如果這是正確的,那麼如何將消息發送到客戶端2?

+0

這個問題應該是一個評論。 – Pol0nium 2013-04-24 16:08:12

2

你正在做的實際實例chatGUI合格後chatGUI的參考您的客戶端類,然後。所以之前通過的引用是不正確的。您需要首先實例化chatGUI和gameGUI對象。然後創建客戶端對象並正確設置引用。不要試圖在構造函數中傳遞引用,因爲那時它們是無效的。