gpt4 book ai didi

JavaFx:同一 View 中两个(或更多)位置的相同标签

转载 作者:行者123 更新时间:2023-11-29 04:12:32 28 4
gpt4 key购买 nike

我不知道我是否错过了什么,但我无法做到这一点,我在同一个 View 上有两次或更多次相同的标签。我真的不想复制它只是使用具有相同文本/工具提示/样式的相同标签。

一个简单的例子:

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.dummy.Controller">
<Label fx:id="myLabel"/>
<!--<Label fx:id="myLabel"/>--> ofc this doesn't work.
<!--<Label fx:id="myLabel"/>-->
</AnchorPane>

如果我尝试使用 <fx:reference>它只出现一次,如果我尝试使用 <fx:copy>它说我需要一个复制构造函数,所以我想我只是错过了一些简单的解决方案。它应该存在。

注意:我真的不想复制这样的代码:myLabel1、myLabel2、...,因为它们都具有相同的文本/工具提示/样式。

我也知道我可以创建一个单独的 .fxml我在其中构建标签并通过 <fx:include> 使用它的文件但我更愿意用同样的方式解决它 .fxml因为这是一件非常简单的事情,所以我认为不值得创建一个新的 .fxml仅此而已。

最佳答案

问题

无法添加相同 Label多次到场景图。这由 Node 记录(强调我的):

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

The scene graph must not have cycles. A cycle would exist if a node is an ancestor of itself in the tree, considering the Group content ObservableList, Parent children ObservableList, and Node clip relationships mentioned above.

If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent. If a program attempts to modify the scene graph in any other way that violates the above rules, an exception is thrown, the modification attempt is ignored and the scene graph is restored to its previous state.

强调部分解释了为什么 Label使用 <fx:reference> 时只出现一次.

解决方案

您的问题的唯一解决方案是复制 Label .您至少可以使用两个选项来完成此操作。

使用<fx:copy>

一种方法是使用 <fx:copy> (强调我的):

The <fx:copy> element creates a copy of an existing element. Like <fx:reference>, it is used with the fx:id attribute or a script variable. The element's "source" attribute specifies the name of the object that will be copied. The source type must define a copy constructor that will be used to construct the copy from the source value.

At the moment, no JavaFX platform classes provide such a copy constructor, so this element is provided primarily for use by application developers. This may change in a future release.

如文档中所述,使用 <fx:copy>要求类有一个复制构造函数。该文档还指出,没有一个核心 JavaFX 类提供复制构造函数。这意味着您必须继承 Label并提供所需的构造函数,如 funkyjelly's answer 所示.为确保属性保持最新,您可以在构造函数中绑定(bind)属性:

public class CopyableLabel extends Label {

public CopyableLabel(CopyableLabel label) {
// You only mentioned the text, tooltip, and style properties
// in your question. Bind more properties as needed.
textProperty().bind(label.textProperty());
tooltipProperty().bind(label.tooltipProperty());
styleProperty().bind(label.styleProperty());
}

}

这样你只需要注入(inject)“master”Label并且对它的任何更新都将传播到所有副本。使用这种设计时,您必须牢记不能直接设置绑定(bind)属性;尝试设置绑定(bind)属性将导致异常。您可能想要记录构造函数将绑定(bind)属性,而不仅仅是复制值。

使用表达式绑定(bind)

另一种选择是在 FXML 文件中绑定(bind)所需的属性。您仍然会创建多个 Label s 但你只需要注入(inject)“master” Label ,和以前一样。

<VBox xmlns="http://javafx.com/javafx/" xmlns:fx="http://javafx.com/fxml/1">

<Label fx:id="master"/>
<Label text="${master.text}" tooltip="${master.tooltip}" style="${master.style}"/>
<Label text="${master.text}" tooltip="${master.tooltip}" style="${master.style}"/>
<Label text="${master.text}" tooltip="${master.tooltip}" style="${master.style}"/>
<!-- repeat as needed -->

</VBox>

这使用了 expression binding JavaFX FXML 的功能。此选项可能会使 FXML 文件困惑,但它不需要您创建 Label 的子类。 .

关于JavaFx:同一 View 中两个(或更多)位置的相同标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54196054/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com