java - 跨 .java 文件创建雷区实例

标签 java javafx

我正在尝试使用 JavaFX 制作雷区游戏。我在创建雷区(板)实例并填充它时遇到困难。在 Controller 中调用 makeMinefield(); 后,minefield 对象仍然是 null

(我没有包含其余的代码,因为它太长并且只包含了应该相关的内容。)

感谢您的任何建议。

在 Minefield.java

package fxMinesweeper;

public class Minefield {

    int minefieldWidth  = 10;           // starts at normal , default values
    int minefieldHeight = 10;           // should change when choicebox changes

    // grid amount of mines
    int gridEasyNumMines = 5;
    int gridNormalNumMines = 50;
    int gridHardNumMines = 190;

    int numMinesatStart;
    int numMinesLeft;
    int cellsUncovered;
    boolean exploded;
    Cell[][] minefield;

    public Minefield(){
        numMinesLeft = 0;
        numMinesatStart = 0;
        cellsUncovered = 0;
        exploded = false;
    }

    public void makeMinefield(){
        minefield = new Cell[minefieldWidth][minefieldHeight];
        for (int i = 0; i < minefieldWidth; i++) {
            for (int j = 0; j < minefieldHeight ; j++ ){
                Cell Cell = new Cell();             // make Cell
                minefield[i][j] = Cell;          
                minefield[i][j].mined = false;
                minefield[i][j].flagged = false;
                minefield[i][j].x = i;
                minefield[i][j].y = j;
                System.out.println("Cell X = "
                      + minefield[i][j].x +": Y = "+ minefield[i][j].y +
                      ": Mined = "+ minefield[i][j].mined +": flagged : " +
                       minefield[i][j].flagged);
            }
        }
    }
}

在Controller.java中

package fxMinesweeper;

import javafx.beans.value.*;
import javafx.collections.FXCollections;
import javafx.event.*;       // in my code I do not have as many .* 's
import javafx.fxml.FXML;     // I used them here to lessen the imports
import javafx.scene.*;
import java.util.Timer;


public class MenuController implements EventHandler<ActionEvent>{
    Minefield minefield;
    public void initialize() {
        System.out.println("Begin Setup");
        Minefield minefield = new Minefield();
        minefield.makeMinefield();
    }
}

最佳答案

// here you declare an instance variable named minefield
Minefield minefield;

public void initialize() {
    System.out.println("Begin Setup");

    // here, instead of initializing the instance variable, 
    // you declare a local variable with the same name
    // it should be: minefield = new Minefield();
    // or: this.minefield = new Minefield();
    Minefield minefield = new Minefield();
    minefield.makeMinefield();
}

关于java - 跨 .java 文件创建雷区实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963574/

相关文章:

java - 包含包装标签的控制皮肤的首选高度

java - 混淆器:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.ObjectMapper

java - 切换 FXML 场景

JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法

java - 为什么我的 InOrder 排序的 ArrayList 不能递归地构建 LinkedBinaryTree?

java - 设计模式 - 验证输入参数并创建持有者类,J2SE(无框架等)

Eclipse Kepler 64 位未在 Windows 7 上运行

Java 模运算符

java - int[] []x[] 如何成为 Java 中的合法数组声明?

DirectoryChooser - 多个目录