共享对象
我实现一个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();
}
}
请注意,此代码是针对更多伪代码引用编写的 - 不想粘贴数百行代码。
任何帮助将不胜感激!非常感谢您的阅读。
假设我正确理解你的问题,我认为这将是有益的你构成你的类有点不同......
每个酒杯对象都有一个客户端,例如
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
}
}
哼,我不知道如果我理解正确的,但它是什么你想要的是:客户端1发送聊天信息给客户机2?
换句话说:客服1 - >客户端1 - >客户机程序 - > GUI2
如果这是正确的,那么如何将消息发送到客户端2?
这个问题应该是一个评论。 – Pol0nium 2013-04-24 16:08:12
你正在做的实际实例chatGUI合格后chatGUI的参考您的客户端类,然后。所以之前通过的引用是不正确的。您需要首先实例化chatGUI和gameGUI对象。然后创建客户端对象并正确设置引用。不要试图在构造函数中传递引用,因为那时它们是无效的。
从服务器类/方法单独的线程上运行的客户端类/方法?如果没有,那么你的服务器将不会从客户端得到任何数据,而不明确地调用它们 – 2013-04-24 15:38:57