![](/img/trans.png)
[英]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.