本文介绍了查看fxml内部的逻辑(特别重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是yfiles开发人员指南中的示例fxml(实际上并不那么重要):

So, here's an example fxml from yfiles developer's guide (not that important actually):

<?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类的对象:

templateNode.item.tag is an object of Person class:

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内进行以下操作:

Is it possible inside fxml to:

a)在fxml中执行一些视图逻辑(这就是我所说的)?例如,要使第一个标签的文本设置为templateNode.item.tag.firstName当且仅当其长度> 10且否则为"whatever"?

a) perform some view-logic (that's how i call it) inside fxml?For example to make first label's text to be set to templateNode.item.tag.firstName if and only if it's length is > 10 and "whatever" otherwise?

b)至少专门遍历模型中的集合?
想象templateNode.item.tag是Person对象的列表.例如,在pydjanvaFX(javaFX内部是django-enhanced-templating,这是我在写此问题时就发明的语言)之类的语言,我可以编写如下内容:

b) at least specifically iterate over a collection from model?
Imagine templateNode.item.tag is a list of Person objects.For example in pydjanvaFX (which is django-enhanced-templating inside javaFX, language i invented on the occasion of writing this question) language i can write something like this:

<?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编写脚本.

You may want to read Introduction to FXML and about Scripting in FXML.

但是,我强烈建议不要这样做.您要查找编译时错误,而不是运行时错误.

关于脚本外观的简短示例,动态添加标签:

A brief example about how a script could look like, a label is added dynamically:

<?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>

我不会对此做任何更深入的介绍,或者列表或您想要执行的任何脚本,因为认真:不要这样!迟早您会遇到问题.

I won't go any deeper into this or lists or whatever scripting you want to do, because seriously: don't do it this way! Sooner or later you'll run into problems.

这篇关于查看fxml内部的逻辑(特别重复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:37