当在 TextField 中键入的文本与图像名称匹配时,如何在 ImageView 中显示图像?
How do I show an image in ImageView when a typed text into TextField matches with the image name?
我在项目文件夹中放了一些图片。每个图像都有一个名称。例如:a、aa、aba 等。当相关图像与输入到 TextField 中的图像名称匹配时,我想将相关图像显示到 ImageView 中。
例如,如果我在 TextField 中键入 'a',它将打开名为 'a' 的图像。如果我键入 "ab",它不会打开任何图像,因为名为 "ab" 的图像文件夹中没有图像。只有当图像的名称与输入到 TextField 中的文本相匹配时,才会显示图像。
我写了一些代码如下-
Application_Controler.java
public class Application_Controler implements Initializable{
@FXML
private TextField txt;
@FXML
private ImageView img;
@Override
public void initialize(URL url, ResourceBundle rb) {
String text=txt.getText();
File file = new File("src/images/"+text);
Image image = new Image(file.toURI().toString());
img.setImage(image);
}
}
这是 .fxml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" 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" prefHeight="55.0" prefWidth="229.0" />
<ImageView fx:id="img" fitHeight="281.0" fitWidth="426.0" layoutX="24.0" layoutY="175.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</children>
</AnchorPane>
有一件事要提一下,在写入 TextField 后不需要按任何额外的 "Enter" 键来显示图像。
您可以使用绑定:
@Override
public void initialize(URL url, ResourceBundle rb) {
img.imageProperty().bind(Bindings.createObjectBinding(() -> {
File file = new File("src/images/"+txt.getText());
if (file.exists()) {
return new Image(file.toURI().toString());
} else {
return null ;
}
}, txt.textProperty());
}
这假设您的路径是正确的(我觉得这很奇怪)并且用户在文本字段中键入了整个文件名(包括文件扩展名,如有必要)。您显然可以根据需要修改 File
构造函数的参数。
我在项目文件夹中放了一些图片。每个图像都有一个名称。例如:a、aa、aba 等。当相关图像与输入到 TextField 中的图像名称匹配时,我想将相关图像显示到 ImageView 中。
例如,如果我在 TextField 中键入 'a',它将打开名为 'a' 的图像。如果我键入 "ab",它不会打开任何图像,因为名为 "ab" 的图像文件夹中没有图像。只有当图像的名称与输入到 TextField 中的文本相匹配时,才会显示图像。
我写了一些代码如下-
Application_Controler.java
public class Application_Controler implements Initializable{
@FXML
private TextField txt;
@FXML
private ImageView img;
@Override
public void initialize(URL url, ResourceBundle rb) {
String text=txt.getText();
File file = new File("src/images/"+text);
Image image = new Image(file.toURI().toString());
img.setImage(image);
}
}
这是 .fxml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" 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" prefHeight="55.0" prefWidth="229.0" />
<ImageView fx:id="img" fitHeight="281.0" fitWidth="426.0" layoutX="24.0" layoutY="175.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</children>
</AnchorPane>
有一件事要提一下,在写入 TextField 后不需要按任何额外的 "Enter" 键来显示图像。
您可以使用绑定:
@Override
public void initialize(URL url, ResourceBundle rb) {
img.imageProperty().bind(Bindings.createObjectBinding(() -> {
File file = new File("src/images/"+txt.getText());
if (file.exists()) {
return new Image(file.toURI().toString());
} else {
return null ;
}
}, txt.textProperty());
}
这假设您的路径是正确的(我觉得这很奇怪)并且用户在文本字段中键入了整个文件名(包括文件扩展名,如有必要)。您显然可以根据需要修改 File
构造函数的参数。