2017-02-22 50 views
1

在FXML訪問到非字符串對象可以說我有一個資源類像這樣:JavaFX的:從資源

public class Resources extends java.util.ListResourceBundle { 

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

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

在我的應用I類加載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對象之外?

編輯:仍然有這個問題。我甚至在我的Resource類中嘗試了{"BAR", new Double(123)}。出現同樣的錯誤,這是真實有道理的,因爲這不是邏輯上修復異常的東西。我想了很多,我很迷茫,因爲我真的想要這樣做,不知道該怎麼做。謝謝你的幫助!

回答

0

所以我張貼在Oracle社區論壇這個問題,並得到了一個答案:https://community.oracle.com/message/14270285#14270285

筆者指的是FXMLLoader source在這裏你可以看到只有getString正在被FXMLLoader調用。因此,按照我想要的方式做這件事根本是不可能的。作者繼續描述,我可以覆蓋FXMLLoader,但重要的部分,resolvePrefixedValue方法,是私人說它是一個'不行'。

最後,我將離開FXMLLoader,因爲它是和與Initializable實施Controller從我Resources類調​​用getObject方法,並增加了在initialize方法的值Polygon工作。這對我來說很好,因爲我腦海中的重要部分是在我的Resources類中定義的值。