有一个问题,javafx对象一旦被放入数组就会丢失引用

编程入门 行业动态 更新时间:2024-10-11 13:30:50
本文介绍了有一个问题,javafx对象一旦被放入数组就会丢失引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我试图在我的控制器类中修改一些圈子,并且我将圈子链接到我的fxml文件圈子但是当我尝试将它们添加到圆圈阵列以便于管理时,他们似乎失去了他们的参考。例如,我有:

So I'm trying to modify some circles in my controller class, and i've linked the circles to my fxml file circles but when i try to add them to a circle array for easy management, they seem to lose their reference. For example I have:

@FXML private Circle circle1 = new Circle(); public void addNumber(ActionEvent event){ circle1.setLayoutX(355.0); circle1.setLayoutY(100.0); circle1.setVisbility(true); }

上述方法成功地在给定坐标中弹出一个圆圈。但是以下方法不起作用:

The above method successfully makes a circle pop up in the given coordinates. However the following doesnt work:

@FXML private Circle circle1 = new Circle(); @FXML private Circle[] c = {circle1}; public void addNumber(ActionEvent event){ c[0].setLayoutX(355.0); c[0].setLayoutY(100.0); c[0].setVisbility(true); }

这不起作用!尝试过阵列试过arraylist尝试过的链表,对circle1的引用就是迷失了。我是JAVAFX的菜鸟,所以这可能是一个简单的修复,但我试图研究一下,尝试了不同的实现,让它工作,我似乎无法找到解决方案。 我们非常感谢任何帮助!

This doesn't work! Tried array tried arraylist tried linkedlist, the reference to circle1 just gets lost. I'm a noob at JAVAFX so this might be a simple fix but i've tried to research a bit, tried different implementations to get it to work and i cant seem to find the solution. Any help would be much appreciated!

推荐答案

这 SSCCE 应提供足够的信息以帮助您入门。

This SSCCE should provide enough information to get you started.

给出以下FXML文件:

Given the following FXML file:

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.shape.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="javafx/javafx/8" xmlns:fx="javafx/fxml/1" fx:controller="yourapplication.FXMLDocumentController"> <children> <Button layoutX="22.0" layoutY="31.0" mnemonicParsing="false" onAction="#addNumber" text="Button" /> <Circle fx:id="circle1" fill="DODGERBLUE" layoutX="56.0" layoutY="118.0" radius="34.0" stroke="BLACK" strokeType="INSIDE" visible="false" /> </children> </Pane>

以下控制器代码:

public class FXMLDocumentController { @FXML private Circle circle1; private Circle[] c; public void initialize() { c = new Circle[] { circle1 }; } public void addNumber(ActionEvent event) { c[0].setLayoutX(355.0); c[0].setLayoutY(100.0); c[0].setVisible(true); } }

你应该得到你想要的结果。

you should get the results you desire.

注意:

  • 使用 FXML 用于将变量绑定到FXML文件中同名元素的注释。
  • 变量不需要 FXML 注释在FXML文件中没有定义。
  • 我不认为数组等基元可以定义为FXML。
  • 对圆圈的引用应该在初始化方法中设置,因为在将变量绑定到FXML文件中的元素之后调用它。
  • 如果你使用 FXML 注释标记 Node 变量,您不需要在控制器代码中设置引用,因为这样做加载FXML文件时会被覆盖(这是问题的根源,请参阅Tomsontoms答案)。
  • Use the FXML annotation to bind your variables to elements of the same name within your FXML file.
  • An FXML annotation is not required for variables that are not defined in the FXML file.
  • I don't think primitives such as arrays can be defined FXML.
  • the reference to the circle should be set in the initialize method as this is called after the variable is bound to the element in the FXML file.
  • if you mark a Node variable with the FXML annotation you don't need to set a reference in the controller code as this will be overwritten when the FXML file is loaded (which is the root of your problem, see Tomsontoms answer).

更多推荐

有一个问题,javafx对象一旦被放入数组就会丢失引用

本文发布于:2023-11-24 23:30:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:就会   数组   有一个   放入   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!