2015-09-02 42 views
2

所以,這裏是從yfiles開發人員指南的例子FXML(不,其實很重要):查看邏輯(尤其是迭代)

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

<?import javafx.scene.layout.StackPane?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.control.Label?> 
<?import com.yworks.yfiles.drawing.NodeTemplate?> 

<NodeTemplate fx:id="templateNode" style="-fx-background-color: darkblue" 
    xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> 
    <VBox alignment="CENTER"> 
     <Label fx:id="firstName" text="${templateNode.item.tag.firstName}" 
      textFill="${templateNode.styleTag.firstNameColor}" /> 

     <Label fx:id="lastName" text="${templateNode.item.tag.lastName}" 
      textFill="${templateNode.styleTag.lastNameColor}" /> 
    </VBox> 
</NodeTemplate> 

templateNode.item.tag是Person類的一個對象:

public class Person { 

    private final String firstName; 
    private final String lastName; 
    public Person(String firstName, String lastName){ 
     this.firstName = firstName; this.lastName = lastName; 
    } 
    public String getFirstName() {return firstName;} 
    public String getLastName() {return lastName;} 
} 

是否有可能內FXML到:

一)執行一些視圖邏輯(這就是我如何稱呼它)裏面FXML? 例如,如果第一個標籤的文本被設置爲templateNode.item.tag.firstName,當且僅當它的長度大於10,否則是「其他」。

b)至少特別重複從模型的集合?
想象一下,templateNode.item.tag是Person對象的列表。 例如在pydjanvaFX(這是內部的JavaFX Django的增強型模板,語言我發明了寫這個問題的場合)語言,我可以寫這樣的事情:

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

<?import javafx.scene.layout.StackPane?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.control.Label?> 
<?import com.yworks.yfiles.drawing.NodeTemplate?> 

<NodeTemplate fx:id="templateNode" style="-fx-background-color: darkblue" 
    xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> 
    <VBox alignment="CENTER"> 
     {% for i, model in enumerate(templateNode.item.tag) %} 
      <Label fx:id="firstName#${i}" text="${model.firstName}" 
       textFill="${templateNode.styleTag.firstNameColor}" /> 

      <Label fx:id="lastName#${i}" text="${model.lastName}" 
       textFill="${templateNode.styleTag.lastNameColor}" /> 
     {% endfor %} 
    </VBox> 
</NodeTemplate> 

回答

2

您可能需要閱讀Introduction to FXMLScripting in FXML

<fx:script>標籤允許呼叫者導入FXML文件中的腳本代碼進入或 嵌入腳本。任何JVM腳本語言都可以使用 ,其中包括JavaScript,Groovy和Clojure等。腳本 代碼通常用於直接在標記或 關聯的源文件中定義事件處理程序,因爲事件處理程序通常可以使用更簡潔的腳本語言編寫爲 ,比使用靜態類型語言如 更簡潔Java的。

但是,我強烈建議不要這樣做。你想找到編譯時錯誤,而不是運行時錯誤。

有關腳本如何能看起來像一個簡單的例子,一個標籤動態添加:

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

<?language javascript?> 
<?import javafx.scene.control.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 


<VBox fx:id="root" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> 
    <children> 
     <Button text="Button" /> 
     <Label text="Label" /> 
    </children> 
    <fx:define> 
     <Label fx:id="addedLabel" text="Label" /> 
    </fx:define> 
    <fx:script> 
     addedLabel.setText('Added Label'); 
     root.getChildren().add(addedLabel); 
     java.lang.System.out.println("Children: " + root.getChildren().size()); 
    </fx:script> 
</VBox> 

我不會去任何深入你想這樣做或列表或任何腳本,因爲嚴重:不要這樣做!遲早你會遇到問題。

+0

好吧,這很令人傷心,因爲大多數教程實際上都傾向於在fxml中聲明相對於java的視圖(並且儘可能多地討厭xml-ish,我實際上可以在這種特殊情況下理解它),現在我被告知它只對最基本的東西。 – piezol

+0

是的,「查看」。但是你正在做的是把代碼放進去。這是完全不同的;-) – Roland

+0

Web框架讓我習慣了完全不同的東西,雖然Django仍然強迫我在單獨的文件中聲明日期格式:v – piezol