java - 如何将文本从 DataOutputStream 发送到 JavaFX 上的 TextField
问题描述
我正在使用 JavaFX 构建一个聊天应用程序,但我不知道如何将字符串从 DataOutputStream 发送到我的 TextField。
我能够使用 JFrame 编写代码,但我想使用 Java FX 来编写代码,但是我无法从另一个窗格访问我的 TextField。有什么建议么?
//Creating Center Pane
public FlowPane getFlowPane2() {
FlowPane centerPane = new FlowPane();
centerPane.setAlignment(Pos.CENTER);
centerPane.setPadding(new Insets(10, 10, 10, 10));
centerPane.setHgap(5.5);
centerPane.setVgap(5.5);
//Creating the node inside Top Pane (TextArea)
TextField textField = new TextField();
textField.setPromptText("Type your message here");
textField.setPrefColumnCount(40);
textField.setText("");
//Placing node inside Top Pane
centerPane.getChildren().add(textField);
return centerPane;
}
//Creating Bottom Pane
public HBox getHBox() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15, 15, 15, 15));
hBox.setAlignment(Pos.BOTTOM_CENTER);
//Creating Nodes inside the pane (Buttons)
Button send = new Button("Send");
send.setOnAction((event) -> {
try {
String msgout = "";
msgout = textField.setText(); //My error is here. I can't access my textField from here
dout.writeUTF(msgout);
} catch(IOException e){
e.printStackTrace();
}
});
Button exit = new Button("Exit");
exit.setOnAction(this);
我希望将字符串从我的 DataOutputStream 发送到我的文本字段,这将显示来自聊天应用程序的消息
解决方案
推荐阅读
- centos7 - 在受限网络中的 cent OS 上进行 yum 更新
- python - 在浮点数列表中仅将零更改为整数
- cmd - dir命令cmd中的特殊字符不起作用
- nativescript - 当我尝试在 Sidekick 中自动生成证书时出现“不支持的帐户”错误
- python-3.x - 熊猫没有正确读取标题
- haskell - Haskell:如何在纯函数中产生副作用
- blockchain - 我在以太坊私有区块链中使用 bootnode 连接了两个笔记,但没有开始挖矿,为什么?
- macos - 如何在不改变代码行为的情况下对 Mac OSX 可执行文件进行代码设计
- c# - C# strange precision lost int to float and backwards
- validation - 是否有可能在 Keras 的时代结束时获得预测?