Java fxml-从属性读取值
我是新来的fxml,我仍在努力解决一些问题。通常,在开发时,我会创建一些用于读取值的*.properties文件。 这是我通常做的:Java fxml-从属性读取值,java,javafx,properties,fxml,Java,Javafx,Properties,Fxml,我是新来的fxml,我仍在努力解决一些问题。通常,在开发时,我会创建一些用于读取值的*.properties文件。 这是我通常做的: <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <l
<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>
文件:${JBOSS_HOME}/standalone/configuration/someproject/properties/project.properties
然后,当我声明我的bean时,我只需读取我想要的任何值。大概是这样的:
<bean id="test" class="my.package.MyClass">
<property name="variable" value="${some.value}" />
</bean>
现在,我一直在寻找如何在fxml中执行类似的操作,但似乎找不到任何东西。
我很抱歉在这件事上对noob表示歉意,但是有可能用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>
PS:我正在尝试开发一个独立的应用程序,但仍然希望在外部配置一些值,而无需在需要更改某些内容时生成新版本
非常感谢。这是可能的,但我还没有看到太多(任何)关于它的文档。诀窍是在加载FXML之前访问
FXMLLoader
,并用属性填充它
给定属性文件application.properties:
foo=bar
你可以做:
Properties properties = new Properties();
Path propFile = Paths.get("application.properties");
properties.load(Files.newBufferedReader(propFile));
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml"));
properties.stringPropertyNames()
.forEach(key -> loader.getNamespace().put(key, properties.getProperty(key)));
Parent root = loader.load();
然后在FXML文件中,您可以使用访问属性,例如
<Label text="${foo}"/>
另一种方法是在FXMLLoader中分配一个资源包
FXMLLoader loader = new FXMLLoader();
loader.setResources(ResourceBundle.getBundle("propertyreader.application"));
Parent root = loader.load(
getClass().getResourceAsStream(
"about.fxml"
)
);
access的语法略有不同(使用%
而不是${
)
about.fxml
您是在JavaFX应用程序中使用Spring,还是这只是一个比较的示例?@James_D这只是一个比较的示例…非常感谢,这正是我想要的。谢谢:-)为什么不简单地说ResourceBundle=ResourceBundle.getBundle(“字符串”);/*strings位于名为strings.properties*/Parent root=fxmloader.load(getClass().getResource(“login.fxml”),bundle)的文件中;然后只需使用%访问密钥名称,如下面的答案?非常感谢您的回答。有了您的回答和James_D的回答,我觉得我可以很好地开发该应用程序。再次感谢:-)
<Label fx:id="version" text="%version"/>
version=1
<?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>
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);
}
}