[英]How do I show an image in ImageView when a typed text into TextField matches with the image name?
[英]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
绑定到textProperty() of TextArea
的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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.