fxml-从属性读取值

Sno*_*nox 2 java javafx properties fxml

我是fxml的新手,但我仍在尝试解决一些问题。通常,在开发时,我会创建一些* .properties文件,用于从中读取值。

这是我通常所做的:

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${JBOSS_HOME}/standalone/configuration/someproject/properties/project.properties
                </value>
            </list>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

然后,当我声明我的豆子时,我只要读取我想要的任何值即可。像这样:

<bean id="test" class="my.package.MyClass">
    <property name="variable" value="${some.value}" />
</bean>
Run Code Online (Sandbox Code Playgroud)

现在,我一直在寻找如何在fxml中执行类似的操作,但是我似乎什么也找不到。我为此感到很抱歉,但是可以用fxml来做这种事情吗?

例如,在下面的代码中,是否可以从外部定义图像的URL:

<VBox alignment="CENTER" styleClass="header" stylesheets="@../styles/some.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane>
         <children>
              <ImageView AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="22.0">
                  <image>
                      <Image url="@../img/image.png" />
                  </image>
              </ImageView>
         </children>
      </AnchorPane>
Run Code Online (Sandbox Code Playgroud)

PS:我正在尝试开发一个独立的应用程序,但是我仍然想在外部配置一些值,而无需在需要更改任何内容时生成新的内部版本。

非常感谢你。

jew*_*sea 5

另一种方法是在FXMLLoader中分配资源束。

FXMLLoader loader = new FXMLLoader();
loader.setResources(ResourceBundle.getBundle("propertyreader.application"));
Parent root = loader.load(
    getClass().getResourceAsStream(
            "about.fxml"
    )
);
Run Code Online (Sandbox Code Playgroud)

访问的语法略有不同(使用%代替${)。

<Label fx:id="version" text="%version"/>
Run Code Online (Sandbox Code Playgroud)

示例应用

该示例应用程序假定所有文件都在名为的程序包中propertyreader

物业读者图片

application.properties

version=1
Run Code Online (Sandbox Code Playgroud)

关于.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="400.0" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <Label text="Property Reader Version:"/>
    <Label fx:id="version" text="%version"/>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
</HBox>
Run Code Online (Sandbox Code Playgroud)

PropertyReaderApp.java

package propertyreader;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ResourceBundle;

public class PropertyReaderApp extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setResources(ResourceBundle.getBundle("propertyreader.application"));
        Parent root = loader.load(
            getClass().getResourceAsStream(
                    "about.fxml"
            )
        );

        stage.setScene(new Scene(root));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)