Java - Eclipse 中未定义的方法
Java - Method Undefined in Eclipse
我目前有一个 3 class java 应用程序,我正在尝试使用 JavaFX 创建一个简单的游戏。在我的 GameCore class 中,我正在尝试创建一个 gameGrid 实例。但是当我使用 "grid = new gameGrid(int, int, int, int);" 时,eclipse 告诉我 gameGrid 未定义并建议我创建该方法,当我按照 eclipse 的要求执行时,它在我的 gameCore class 中放置了一个私有方法 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 完成的网格,您可以查看 我最近问的问题。
我目前有一个 3 class java 应用程序,我正在尝试使用 JavaFX 创建一个简单的游戏。在我的 GameCore class 中,我正在尝试创建一个 gameGrid 实例。但是当我使用 "grid = new gameGrid(int, int, int, int);" 时,eclipse 告诉我 gameGrid 未定义并建议我创建该方法,当我按照 eclipse 的要求执行时,它在我的 gameCore class 中放置了一个私有方法 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 完成的网格,您可以查看