[英]View logic inside fxml (iterating in particular)
因此,這是 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中:
a) 在 fxml 中執行一些視圖邏輯(我就是這么稱呼它的)? 例如,將第一個標簽的文本設置為 templateNode.item.tag.firstName 當且僅當其長度大於 10 時,否則為“無論如何”?
b) 至少專門迭代模型中的集合?
想象一下 templateNode.item.tag 是一個 Person 對象列表。 例如,在 pydjanvaFX(這是 javaFX 中的 django-enhanced-templating,我在寫這個問題時發明的語言)語言中,我可以寫這樣的東西:
<?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>
您可能需要閱讀FXML 簡介和 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>
我不會更深入地研究這個或列表或任何你想做的腳本,因為說真的:不要這樣做! 遲早你會遇到問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.