java - 为什么对象数组中的元素没有更新?

标签 java javafx

在使用 JavaFX 创建应用程序时,我遇到了一个小问题 - 我的数组中的元素不知何故没有随着对这些元素所做的更改而更新。首先我想指出 - 不要关注我的应用程序的结构和模型 - 我已经知道它不好并且我改变了它,但我仍然不明白为什么我的问题存在。我的数组的初始化如下所示:

public class GameBoardAI implements IGameModel{


Random rand = new Random();
int currentPlayer = 1;
TicTacViewController tacController = new  TicTacViewController();
Button[] buttonss = new Button[]{ tacController.btn1, tacController.btn2, tacController.btn3, tacController.btn4, 
        tacController.btn5, tacController.btn6, tacController.btn7, tacController.btn8, tacController.btn9};  

问题是,当我创建按钮数组时,按钮尚未连接到 View ,因此它们仍然为空。当我尝试调用内部按钮上的某些方法时,我遇到了一个问题:

public void switchPlayer() {
if(currentPlayer == 1)
{              
    currentPlayer=2;
    buttonss[rand.nextInt(9)].fire();

}
if(currentPlayer == 2)
    currentPlayer = 1;

}

您可以在这里看到,我正在尝试从我在实例变量中创建的按钮数组中获取一些随机按钮。这是当按钮位于 TicTacViewController 内部时的代码部分:

    public class TicTacViewController implements Initializable
{

@FXML
private Label lblPlayer;

@FXML
private Button btnNewGame;

@FXML
private GridPane gridPane;

private static final String TXT_PLAYER = "Player: ";
private IGameModel game = new GameBoard();
@FXML
public Button btn1;
@FXML
public Button btn2;
@FXML
public Button btn3;
@FXML
public Button btn4;
@FXML
public Button btn5;
@FXML
public Button btn6;
@FXML
public Button btn7;
@FXML
public Button btn8;
@FXML
public Button btn9;

据我了解,问题是,当我创建数组作为实例变量时,mu 按钮仍然为空 - 它们尚未连接到 View 。但这里发生了一些奇怪的事情:当我将数组初始化放在 switchPlayer 方法中而不是作为实例变量执行时 - 一切都正常工作。所以看起来当我在调用方法时创建数组时按钮已经连接到 View 并且没有问题。它破坏了我关于引用变量的知识 - 为什么当我们创建数组作为实例变量时它不起作用?因为我认为即使我们在数组中有一个引用变量 - 当我们更改此引用变量时,它们也会在我们的数组中更改。更具体地说 - 即使当我们初始化数组并且按钮尚未连接到 View 时,它们也会在之后连接 - 因此当我们调用 switchPlayer 方法时按钮应该已经连接到 View - 但编译器告诉我他们是空的。有人可以解释一下这里有什么问题吗?为什么在调用方法时按钮仍然为空,因为它们在数组创建中,即使它们后来连接到 View ?

最佳答案

Java 是一种按值传递语言。对象类型变量不是对象指针,它们仅保存对象引用(即内存地址)。示例:

String str = "Hello"; // A String type variable holding reference of "Hello" string
String str2 = str; // Variable "str2" now copies the reference of "str"
String str2 = "World"; // Variable "str2" changes the reference it holds to the string "World" (in other word, it is being replaced)

它经常被混淆,因为以下内容是有效的:

List<String> foo = new ArrayList<>(); // Let foo hold the reference of an empty arraylist
List<String> bar = foo; // Let bar hold the reference that is held by foo
bar.add("hello");
System.out.println(foo); // Prints "[hello]"

这是有效的,因为 bar 已从 foo 复制了 ArrayList 的对象引用,因此对 ArrayList< 的任何操作 通过 bar 将被 foo 反射,因为它们都持有相同的对象引用。

回到你的问题。如果tacController尚未加载FXML文件,则所有Button引用将为null。因此,您要做的就是复制 null 引用并将这些 null 引用保留在数组中。因此,您将永远无法访问实际的 Button 对象。

关于java - 为什么对象数组中的元素没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52943479/

相关文章:

css - 将 CSS 样式属性绑定(bind)到 JavaFX 中的节点

java - JVM TI 的链接库名称

java - Spark Web 框架单元测试

JavaFX,在堆栈 Pane 上定位单选按钮的问题

java - 在 JavaFX 中添加按钮后工具栏的大小不正确

javascript - JavaFX : Set default printer for WebEngine/Webview

javascript - JavaFX Webview 不支持 window.FileReader javascript 的解决方法

java - NullPointerException:println 需要一条消息

java - Nutch - 无法从资源 org/sonar/ant/antlib.xml 加载定义

Java:根据枚举中的列对 JTable 进行排序