图像 URL 的 FXML 变量标记
FXML Variable Tagging for Image URLs
我正在做一个项目,我需要在特定目录中的多个图像之间切换,以用作我的程序的背景。不幸的是,我不能将所有图像路径硬编码到 fxml 中作为 urls 并使用 fxids 在每个路径之间切换,因为我有太多图像,而且整个星期都会添加新图像。我创建了一个 fxid,它链接到保存图像路径的特定变量。此变量是一个 url,其路径为“@../images/Planets/image1.png”。 fxml 正确加载按钮元素,但无法加载与给定变量关联的链接 url。我取了确切的 url 并将其直接放入 fxml 中,从而正确显示了一张图像。我需要帮助才能在 fxml 中正确识别 url 变量。下面是我的 fxml 代码。
<AnchorPane fx:id="AP" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.controllers.PlanetController">
<children>
<fx:define>
<URL fx:id="image"/>
</fx:define>
<ImageView fitHeight="721.0" fitWidth="1289.0">
<image>
<Image url="$image"/>
</image>
</ImageView>
<Button fx:id="map" layoutX="709.0" layoutY="685.0" mnemonicParsing="false" onAction="#handleNext" text="Map" />
<Button layoutX="428.0" layoutY="690.0" mnemonicParsing="false" text="Market" />
</children>
</AnchorPane>
这是加载 fxml 文档的 javafx 代码。忽略 rootController。它将场景设置为 vBox。
@FXML
private URL image;
@FXML
public void handleTravelToRegion() {
image = new URL("@../images/Planets/image4.jpg");
try {
URL url = this.getClass().getResource(getScene("destinationPlanet"));
FXMLLoader loader = new FXMLLoader(url);
Node scene = loader.load();
rootController.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
请帮我获取 <Image url="$image"/>
以加载图像变量。非常感谢您的帮助。
如果其他人在将节点设置为他们的 fxml 文档中的变量时遇到困难,请转到您的控制器中的初始化方法 class 并将您想要的节点添加到您的 fxml 文档中链接的窗格中。在我的例子中,我在我的 fxml 文档中使用了一个 anchorPane,我使用 @FXML 标签链接到我的控制器 class。我需要将包含随机图像文件的 ImageView 节点添加到我的定位窗格。我需要做的就是在 FXML initialize 方法中添加节点,否则,我的场景不会显示任何更改。下面是我的 fxml 文档和控制器的最终结果 class:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="ap" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.controllers.PlanetController">
在我的控制器 class 中,我需要执行以下操作:
public class PlanetController implements Initializable {
private ImageView background = new ImageView();
@FXML
private AnchorPane ap = new AnchorPane();
public void initialize(URL url, ResourceBundle resourceBundle) {
Random rand = new Random();
File[] imageArray = new File("@../images/Planets/").listFiles();
Image image = new Image(imageArray[rand.nextInt(imageArray.length)]
.toURI().toString());
background = new ImageView(image);
ap.getChildren().add(background);
}
这就是让背景正常工作所要做的一切。
我正在做一个项目,我需要在特定目录中的多个图像之间切换,以用作我的程序的背景。不幸的是,我不能将所有图像路径硬编码到 fxml 中作为 urls 并使用 fxids 在每个路径之间切换,因为我有太多图像,而且整个星期都会添加新图像。我创建了一个 fxid,它链接到保存图像路径的特定变量。此变量是一个 url,其路径为“@../images/Planets/image1.png”。 fxml 正确加载按钮元素,但无法加载与给定变量关联的链接 url。我取了确切的 url 并将其直接放入 fxml 中,从而正确显示了一张图像。我需要帮助才能在 fxml 中正确识别 url 变量。下面是我的 fxml 代码。
<AnchorPane fx:id="AP" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.controllers.PlanetController">
<children>
<fx:define>
<URL fx:id="image"/>
</fx:define>
<ImageView fitHeight="721.0" fitWidth="1289.0">
<image>
<Image url="$image"/>
</image>
</ImageView>
<Button fx:id="map" layoutX="709.0" layoutY="685.0" mnemonicParsing="false" onAction="#handleNext" text="Map" />
<Button layoutX="428.0" layoutY="690.0" mnemonicParsing="false" text="Market" />
</children>
</AnchorPane>
这是加载 fxml 文档的 javafx 代码。忽略 rootController。它将场景设置为 vBox。
@FXML
private URL image;
@FXML
public void handleTravelToRegion() {
image = new URL("@../images/Planets/image4.jpg");
try {
URL url = this.getClass().getResource(getScene("destinationPlanet"));
FXMLLoader loader = new FXMLLoader(url);
Node scene = loader.load();
rootController.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
请帮我获取 <Image url="$image"/>
以加载图像变量。非常感谢您的帮助。
如果其他人在将节点设置为他们的 fxml 文档中的变量时遇到困难,请转到您的控制器中的初始化方法 class 并将您想要的节点添加到您的 fxml 文档中链接的窗格中。在我的例子中,我在我的 fxml 文档中使用了一个 anchorPane,我使用 @FXML 标签链接到我的控制器 class。我需要将包含随机图像文件的 ImageView 节点添加到我的定位窗格。我需要做的就是在 FXML initialize 方法中添加节点,否则,我的场景不会显示任何更改。下面是我的 fxml 文档和控制器的最终结果 class:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="ap" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.controllers.PlanetController">
在我的控制器 class 中,我需要执行以下操作:
public class PlanetController implements Initializable {
private ImageView background = new ImageView();
@FXML
private AnchorPane ap = new AnchorPane();
public void initialize(URL url, ResourceBundle resourceBundle) {
Random rand = new Random();
File[] imageArray = new File("@../images/Planets/").listFiles();
Image image = new Image(imageArray[rand.nextInt(imageArray.length)]
.toURI().toString());
background = new ImageView(image);
ap.getChildren().add(background);
}
这就是让背景正常工作所要做的一切。