如何将数据从DataOutputStream发送到JavaFX上的TextField

时间:2019-07-20 04:32:52

标签: java events javafx

我正在使用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发送到文本字段,该文本字段将显示来自聊天应用程序的消息

0 个答案:

没有答案