【问题标题】:JavaFX: Access in FXML to a non-String-Object from a ResourceBundleJavaFX:以 FXML 访问 ResourceBundle 中的非字符串对象
【发布时间】:2017-02-22 14:41:10
【问题描述】:

假设我有一个这样的资源类:

public class Resources extends java.util.ListResourceBundle {

    private static final Object[][] OBJECTS = new Object[][]{
        {"FOO", "foo"},
        {"BAR", 123d}
    }

    @Override
    protected Object[][] getContents() {
        return OBJECTS;
    }
}

在我的应用程序类中,我像这样加载fxml

Resources resources = new Resources();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo.fxml"), resources);
Parent root = loader.load();

在我的foo.fxml 中,我想同时使用我的字符串和我的双资源值,如下所示:

<Label text="%FOO">
<Polygon>
    <points>
        <Double fx:value="0"/>
        <Double fx:value="0"/>
        <Double fx:value="%BAR"/>
        <Double fx:value="0"/>
    </points>
</Polygon>

第一行完美运行,但带有fx:value="%BAR" 的行创建异常如下:

javafx.fxml.LoadException: 
file:/foo.fxml:90
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1143)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at my Application class in line Parent root = loader.load();
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NumberFormatException: For input string: "%BAR"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:450)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:982)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 17 more

所以我的问题是:如何在 FXML 中使用 String 以外的资源对象?

编辑:还是有这个问题。我什至在我的资源类中尝试了{"BAR", new Double(123)}。出现了同样的错误,老实说这是有道理的,因为这在逻辑上无法修复异常。我想了很多,我在这里很迷茫,因为我真的希望它能够工作并且不知道该怎么做。感谢您的帮助!

【问题讨论】:

    标签: java javafx fxml resourcebundle


    【解决方案1】:

    所以我在 Oracle 社区论坛上发布了这个问题并得到了答案:https://community.oracle.com/message/14270285#14270285

    作者参考FXMLLoader source,您可以看到FXMLLoader 只调用了getString。因此,根本不可能以我想要的方式做到这一点。作者继续描述我可以覆盖FXMLLoader,但重要的部分,resolvePrefixedValue 方法,私下说它是“不行的”。

    最后,我将保留 FXMLLoader 原样并使用实现 ControllerInitializable,它从我的 Resources 类调用 getObject 方法并将值添加到 @987654333 中的 Polygon @ 方法。这对我来说很好,因为我认为重要的部分是在我的 Resources 类中定义值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      相关资源
      最近更新 更多