Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 图像URL的FXML变量标记_Image_Url_Javafx_Fxml - Fatal编程技术网

Image 图像URL的FXML变量标记

Image 图像URL的FXML变量标记,image,url,javafx,fxml,Image,Url,Javafx,Fxml,我正在从事一个项目,在这个项目中,我需要在特定目录中的多个图像之间切换,以用作我的程序的背景。不幸的是,我无法将所有图像路径硬编码为fxml作为URL,并使用FXID在每个路径之间切换,因为我有太多的图像,并且一周中都会添加新的图像。我创建了一个fxid,它链接到一个特定的变量,该变量保存图像的路径。此变量是一个url,其路径为“@../images/plants/image1.png”。fxml正确加载按钮元素,但无法加载与给定变量关联的链接url。我获取了准确的url并将其直接放入fxml中

我正在从事一个项目,在这个项目中,我需要在特定目录中的多个图像之间切换,以用作我的程序的背景。不幸的是,我无法将所有图像路径硬编码为fxml作为URL,并使用FXID在每个路径之间切换,因为我有太多的图像,并且一周中都会添加新的图像。我创建了一个fxid,它链接到一个特定的变量,该变量保存图像的路径。此变量是一个url,其路径为“@../images/plants/image1.png”。fxml正确加载按钮元素,但无法加载与给定变量关联的链接url。我获取了准确的url并将其直接放入fxml中,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文档中的变量时遇到困难,请转到控制器类中的初始化方法,并将所需节点添加到fxml文档中链接的窗格中。在我的例子中,我在fxml文档中使用了anchorPane,我使用@fxml标记将它链接到我的控制器类。我需要将包含随机图像文件的ImageView节点添加到锚定窗格中。我所需要做的就是在FXML初始化方法中添加节点,否则,不会显示对场景的任何更改。下面是我的fxml文档和控制器类的最终结果:

<?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">
这就是我所要做的,让背景工作

<?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">
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);
}