Java - Eclipse 中未定义的方法

标签 java javafx

我目前有一个 3 类 java 应用程序,我正在尝试使用 JavaFX 创建一个简单的游戏。在 My GameCore 类中,我尝试创建 gameGrid 的实例。但是当我使用“grid = new gameGrid(int, int, int, int);”时eclipse 告诉我 gameGrid 未定义并建议我创建该方法,当我按照 eclipse 的要求执行操作时,它会在我的 gameCore 类中放置一个私有(private)方法 gameGrid,但 gameGrid 应该是 gameGrid.class 的构造函数。我已经重新启动了项目并清理了项目,但没有效果。

public class gameCore {

    gameGrid grid;

    public gameCore(){
        getGrid();
    }

    public void getGrid(){
        grid = gameGrid(32, 32, 10, 10); //Error is here, underlining "gameGrid"
//Also using gameGrid.gameGrid(32,32,10,10); does not work either, still says its undefined

/*
This is the code that Eclipse wants to place when I let it fix the error, and it places this code in this class.
private gameGrid gameGrid(int i, int j, int k, int l) {
        // TODO Auto-generated method stub
        return null;
    }
*/

    }

}
<小时/>
public class gameGrid {

    protected int[][] grid;
    protected int tileWidth;
    protected int tileHeight;

    public gameGrid(int tileWidth, int tileHeight, int horizTileCount, int vertTileCount){
        //Create Grid Object

        grid = new int[vertTileCount][];
        for(int y = 0; y < vertTileCount; y++){
            for(int x = 0; x < horizTileCount; x++){
                grid[y] = new int[horizTileCount];
            }
        }

        this.tileWidth = tileWidth;
        this.tileHeight = tileHeight;
    }
}
<小时/>
import java.awt.Dimension;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class gameGUI extends Application {

    Dimension screenDimensions = new Dimension(java.awt.Toolkit.getDefaultToolkit().getScreenSize());

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage stage) throws Exception {
        Canvas c = new Canvas();
        StackPane sp = new StackPane();
        Scene scene = new Scene(sp, screenDimensions.width, screenDimensions.height);

        sp.getChildren().add(c);
        stage.setScene(scene);

        gameCore game = new gameCore();



        stage.show();


    }

}

最佳答案

您缺少的是实例化的"new",即。 e.你需要写

grid = new gameGrid(32, 32, 10, 10); 

在以大写字符开头的 Java 类中,您应该 read the guidelines .

如果您想查看在 JavaFX 中使用 java 节点而不是 Canvas 完成的网格,您可以查看 the code我最近问的问题。

关于Java - Eclipse 中未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30364705/

相关文章:

java - JavaFX 运行时 JAR 文件 jfxrt.jar 在 Linux 上的位置是什么?

java - 尝试从方法返回两个值

java - 尝试使用 Modbus 协议(protocol)连接设备时出现 net.wimpi.modbus.ModbusSlaveException : Error Code = 2,

java - 与sql like 和bind 精确匹配

java - 如何清除JavaFX GraphicsContext中的路径?

java - 按属性值动态过滤 ObservableList

java - Java FX 中的 "mnemonicParsing"属性是什么

java - 有效地将java字符串转换为其等效的枚举

java - 关于使用 wav 文件进行编程以便对其应用 dpcm

java - 需要实例化的类的 Spring @Autowire 属性