首页 > 解决方案 > 如何将文本从 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 发送到我的文本字段,这将显示来自聊天应用程序的消息

标签: javaeventsjavafx

解决方案


推荐阅读