简体   繁体   English

如何从fxml文件中的xml文件读取值?

[英]How can I read values from an xml file in a fxml file?

I am newbie to JavaFX, and as a similar way that is done in Android Studio, I would like to have a file called strings.xml where I can store some resources that can be read from an fxml file . 我是JavaFX的新手,并且与Android Studio中的操作类似,我希望有一个名为strings.xml的文件,其中可以存储一些可以从fxml文件读取的资源。 For example, if I have the following code in strings.xml : 例如,如果我在strings.xml中具有以下代码:

<resources>
    <string name="app_name">My App Name</string>
</resources>

I would like to do something like this in the fxml file: 我想在fxml文件中执行以下操作:

text="@string/app_name"

How is it possible to achieve that? 如何做到这一点?

Yes, it is possible to use internationalized strings within FXML files in a way similar to Android's resources. 是的,可以以类似于Android资源的方式在FXML文件中使用国际化字符串。 There are some differences: 有一些区别:

  1. The resource files are .properties files instead of .xml . 资源文件是.properties文件而不是.xml The variables are defined in a different way too: 变量的定义方式也不同:

     variable.name_1=Hello variable.name_2=World 

    (Note missing <resource> tags, it is just plain [var_name]=[value] ). (请注意缺少<resource>标签,它只是普通的[var_name]=[value] )。

  2. To reference a string in a FXML file, you use a % instead of an @string/ . 要引用FXML文件中的字符串,请使用%而不是@string/ For example: 例如:

     <Button fx:id="button1" text="%variable.name_1" /> 

That's it. 而已。 I recommend searching for ResourceBundle as this explains how the property files should be named for the right language to be chosen. 我建议搜索ResourceBundle因为这说明了如何为所选的正确语言命名属性文件。 Also, that bundle can be used for adding internationalized variables to your code as well. 而且,该捆绑包还可用于将国际化变量添加到您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM