繁体   English   中英

在TextField中键入每个字符时,如何显示在TextArea中?

[英]How do I show each character in TextArea when it is typed in TextField?

我试图在将每个字符键入TextField时立即将其显示在TextArea中。

我写了一些代码,如下所示,但没有用:

Application_Controler.java

public class Application_Controler {

    @FXML
    private TextField txt;

    @FXML
    private TextArea showTxt;

    @FXML
    void keyTyped(ActionEvent event) {
        String text = txt.getText();
        String oldText = showTxt.getText();
        String nextText = oldText+text;
        showTxt.setText(nextText);
    }

}

这是.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="imran.jfx.application.Application_Controler">
   <children>
      <AnchorPane layoutX="-17.0" layoutY="-14.0" prefHeight="461.0" prefWidth="454.0">
         <children>
            <TextField fx:id="txt" layoutX="122.0" layoutY="87.0" onKeyTyped="#keyTyped" prefHeight="55.0" prefWidth="229.0" />
            <TextArea fx:id="showTxt" layoutX="56.0" layoutY="215.0" prefHeight="210.0" prefWidth="362.0" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

我该怎么办?

您可以将textProperty() of TextFieldtextProperty() of TextArea绑定到textProperty() of TextAreatextProperty() of TextArea

textArea.textProperty().bind(textField.textProperty());

尝试在控制器的initialize()内部添加以下代码:

public class Application_Controler implements Initializable {

    @FXML
    private TextField txt;

    @FXML
    private TextArea showTxt;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
         showTxt.textProperty().bind(txt.textProperty());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM