在 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 TextField
绑定到 textProperty() 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());
}
}
我试图在输入 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 TextField
绑定到 textProperty() 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());
}
}