聊天Messenger应用设计问题? JavaFX

时间:2018-07-24 22:45:42

标签: java javafx chat messenger

我目前正在为游戏玩家开发交易市场计划。对于玩家将用来进行通信的Messenger部分,我只对如何设置有几个问题。

  • Messenger窗口将使用一个ListView(名为“ chatListView”)以及一个用于“聊天”对象的单元格工厂

  • “聊天”对象将具有一个字符串getOriginUsername(),它将是ListView单元格本身中显示的文本

  • Messenger窗口将在“聊天”对象内的字符串数组列表中存储消息的文本区域(名为“ chatDisplay”)

  • 客户端收到消息后,会将消息添加到匹配的“聊天”对象的ArrayList

  • 然后该消息应显示在文本区域

我的问题是..我如何使Messenger意识到已将新字符串添加到ArrayList,并知道是否要更新文本区域? 'Chat'对象具有在'Chat'中相应的ArrayList附加一条消息的方法,但是ListView已填充'Chat'。由于已经填充了它,因此它不会监听每个“聊天”对象内部的更改。我如何实现这样的东西?

因为ListView单元格本身需要监听更改(如果选择了该单元格,换句话说就是如果..聊天打开并且正在进行聊天),则需要在其中添加某种侦听器细胞本身,对吧?

例如。使用聊天对象方法追加到“聊天”的消息数组列表中,对入站消息执行以下操作:

 chatListView.getSelectionModel().getSelectedItem().appendMessage("This is a message");

然后,将附加消息显示在主消息文本区域“ chatDisplay”中。

我该怎么做呢?我只是通过搜索无法真正找到想要的东西。或更可能是我无法想到正确的措词来进行正确的搜索。

聊天类:

package tarkov.trader.objects;
import java.util.ArrayList;

public class Chat extends Form {


public boolean isNew;
private String origin;   // This string will be a client's username that initiated the chat
private String destination;   // This string will be a client's username receiving the new chat
private ArrayList<String> messages;   // The arraylist will hold strings of messages between clients


public Chat(String origin, String destination, ArrayList<String> messages)
{
    this.type = "chat";
    this.isNew = true;
    this.origin = origin;
    this.destination = destination;
    this.messages = messages;
}


// Getters:
public String getOrigin()
{
    return origin;
}


public String getDestination()
{
    return destination;
}


public ArrayList<String> getMessages()
{
    return messages;
}


public String getName(String clientName)
{
    if (origin.equals(clientName))
        return destination;
    else
        return origin;
}



// Setters:
public void setOpened()   // Server calls this upon receiving and recognizing the chat is 'new'
{
    this.isNew = false;
}


public void setMessages(ArrayList<String> messages)
{
    this.messages = messages;
}


public void appendMessage(String message)
{
    if (this.messages == null)
    {
        this.messages = new ArrayList<>();
    }

    this.messages.add(message);
}

}

这是我的列表视图单元格工厂..并开始进行操作以解压缩消息的数组列表,然后在文本区域“ chatDisplay”中显示

        // List view 
    chatListView = new ListView<>();
    chatListView.setCellFactory(param -> new ListCell<Chat>() {
        @Override
        protected void updateItem(Chat chat, boolean empty)
        {
            super.updateItem(chat, empty);

            if (empty || chat == null)
            {
                this.setText(null);
                this.setGraphic(null);
            }
            else
            {
                this.setText(chat.getName(TarkovTrader.username));
                this.setGraphic(new ImageView(new Image(this.getClass().getResourceAsStream("/chat.png"), 24, 24, true, true)));
                this.setPrefHeight(45);
                });
            }
        }
    });
chatListView.setOnMouseClicked(e -> {
 unpackChatMessages(chatListView.getSelectionModel().getSelectedItem().getMessages());

});

最好采取unpackChatMessages方法:

private boolean unpackChatMessages(ArrayList<String> messageList)
{
    chatDisplay.setText(null);

    if (messageList == null)
        return false;

    for (String message : messageList)
    {
        append(message);
    }

    return true;
}

0 个答案:

没有答案