gpt4 book ai didi

java - 使用绑定(bind)创建/改进伪终端

转载 作者:行者123 更新时间:2023-12-01 11:29:27 24 4
gpt4 key购买 nike

我的目标是创建一个伪终端,它将显示我收到的数据。为了创建伪终端,我使用绑定(bind)到 StringPropertyTextArea。当我的 StringProperty 属性更改时,我的 TextArea 必须更新其内容。

我实现了一个有效的第一个版本,它显示我收到的每条消息,但是每次收到新数据时都会删除 TextArea 的内容

版本 1:

In the class that receives the data :

StringProperty sendData = new SimpleStringProperty();
WHEN I RECEIVE DATA : sendData.set(Arrays.toString(strings));

In the class that contains the TextArea :

@FXML private TextArea consoleTextArea;
public void setLiaison (StringProperty textRecu){
consoleTextArea.textProperty().bind(textRecu);
}

In a third class I initialise everything, and call :

Controller.setLiaison(sendData);

不希望每次都清除我的textArea我当然收到数据,所以我尝试这样做:

consoleTextArea.textProperty().bind(Bindings.concat(consoleTextArea.getText()).concat("\n").concat(textRecu.get()));

但这根本不起作用,它只显示我在 TextArea 上收到的第一条消息,然后什么也不显示。

还有其他方法可以使用绑定(bind)保留 TextArea 中的内容吗?

NB :我无法使用附加文本之类的简单方法,因为我使用 MVC,并且我的 Controller (TextArea)必须是链接到我的模型(StringProperty)。

最佳答案

要么StringProperty存储连接在一起的所有消息(而不是您当前拥有的 sendData.set(...) 方法):

sendData.set(sendData.get() + Arrays.toString(strings));

每次属性更改时都通过串联更新文本区域(而不是您当前拥有的绑定(bind)):

textRecu.addListener((obs, oldText, newText) -> consoleTextArea.appendText(newText));

使用这两个版本中的任何一个,如果您输入大量文本,您的性能最终会受到影响(因为字符串连接基本上是一个缓慢的过程)。例如,使用 ObservableList<String> 可能会更好。保存所有消息,以及 ListView<String>显示它们,如果列表开始变得太满,则删除较早的消息。

关于java - 使用绑定(bind)创建/改进伪终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com